tags:

views:

54

answers:

3

I am reading data in from a url, parsing it, and then attempting to format the data further:

year = 2008;
month = 9;
day = 30;

raw = urlread(sprintf('http://www.wunderground.com/history/airport/KCVS/%i/%i/%i/DailyHistory.html?HideSpecis=0&theprefset=SHOWMETAR&theprefvalue=0&format=1',year,month,day));
data = textscan(raw,'%s %s %s %s %s %s %s %s %s %s %s %s','Delimiter',',','HeaderLines',2,'CollectOutput',true);

dir = data{1}(1:end-1,7);
wind = cellfun(@str2num,data{1}(1:end-1,8),'UniformOutput',false);
gust = cellfun(@str2num,data{1}(1:end-1,9),'UniformOutput',false);

wind{cellfun(@isempty,wind)} = 0;
gust{cellfun(@isempty,gust)} = 0;

Now wind{cellfun(@isempty,wind)} = 0; works however gust{cellfun(@isempty,gust)} = 0; does not, instead I get this error that says: ??? The right hand side of this assignment has too few values to satisfy the left hand side. cellfun(@isempty,gust) is properly returning a logical array. Also gust{1} = 0 will work. Why does it work for wind but not gust?

+1  A: 

The braces make the difference:

wind(cellfun(@isempty,wind)) = {0};
gust(cellfun(@isempty,gust)) = {0};


Extract from Cell Arrays and Their Contents

Use curly braces {} for setting or getting the contents of cell arrays.

Use parentheses () for indexing into a cell array to collect a subset of cells together in another cell array.

zellus
+2  A: 

The reason that wind{cellfun(@isempty,wind)} works but gust{cellfun(@isempty,wind)} does not is simply that wind happens to have only one nonempty element. As for the real issue, indexing a cell array with braces returns the elements of the cells indexed; when used with a non-scalar index, such as a logical array, you essentially return the value of each element, one at a time (you can see that the ans variable is being overwritten 33 times). Instead, you must use parentheses to index the array, which say to return the cells of a cell array, and overwrite the elements of the array -- the cells -- with a cell containing what you want. Hence

wind(cellfun(@isempty,wind)) = {0};
gust(cellfun(@isempty,gust)) = {0};
ascent1729
I was just starting to notice the `ans` was being overwritten 33 times before you answered. Good catch!
Elpezmuerto
+4  A: 

Here's a slightly better way to parse the data:

year = 2008; month = 9; day = 30;

%# get raw data
urlStr = sprintf('http://www.wunderground.com/history/airport/KCVS/%i/%i/%i/DailyHistory.html?HideSpecis=0&theprefset=SHOWMETAR&theprefvalue=0&format=1',year,month,day);
raw = urlread(urlStr);

%# collect data and headers
raw = strrep(raw, '<br />', '');        %# remove HTML <br/> at end of each line
raw = textscan(raw,repmat('%s ',1,12), 'Delimiter',',', 'HeaderLines',1, 'CollectOutput',true);
headers = raw{1}(1,:);
data = raw{1}(2:end-1,:);

%# extract certain columns
A = data(:,7);                %# cell array of strings
B = str2double(data(:,8:9));  %# numeric data
B( isnan(B) ) = 0;

where:

>> B
B =
          5.8            0
          5.8            0
          5.8            0
            0            0
            0            0
          5.8            0
          4.6            0
            0            0
          3.5            0
          4.6            0
          6.9            0
          9.2         17.3
         12.7         20.7
         13.8         19.6
           15            0
         11.5            0
         11.5            0
          9.2            0
          8.1            0
          9.2            0
          9.2            0
          9.2            0
         10.4            0
         10.4            0
Amro

related questions