matlab - Matlab 中的单元格逻辑索引问题

标签 matlab

我正在从 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/

相关文章:

matlab - 将一组 (X, Y, Z) 点绘制为一个漂亮的封闭对象?

matlab - 间接(误差状态)卡尔曼滤波器的结构是什么?误差方程是如何导出的?

matlab - 在纸上以精确尺寸打印 MATLAB 图

c++ - 墨西哥 : stackoverflow error 中的 openmp

image - MatLab - 使用 FFT 移动图像

function - 带有子函数调用的奇怪 'while' 循环行为(MATLAB)

java - 当我尝试使用 java 包 jdde 时 MATLAB 挂起,但只是在系统重新启动后第一次挂起

MATLAB:switch 语句问题

arrays - 从索引数组创建二进制矩阵

python - python 是否有 Matlab 的 `ans` 变量捕获未存储在任何变量中的返回值?