我正在从 url 中读取数据,对其进行解析,然后尝试进一步格式化数据:
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;
现在 wind{cellfun(@isempty,wind)} = 0;
有效,但是 gust{cellfun(@isempty,gust)} = 0;
无效,相反我收到此错误消息:???此赋值的右侧值太少,无法满足左侧。 cellfun(@isempty,gust)
正确返回了一个逻辑数组。 gust{1} = 0
也将起作用。 为什么它对风有效而不对阵风有效?
最佳答案
这里有一个稍微好一点的数据解析方法:
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;
哪里:
>> 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
关于matlab - Matlab 中的单元格逻辑索引问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3816752/