【问题标题】:Problem with cell logical indexing in MatlabMatlab中单元格逻辑索引的问题
【发布时间】:2023-03-10 17:58:01
【问题描述】:

我正在从 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 也可以。 为什么它适用于风而不适用于阵风?

【问题讨论】:

    标签: matlab


    【解决方案1】:

    这是解析数据的更好方法:

    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
    

    【讨论】:

      【解决方案2】:

      wind{cellfun(@isempty,wind)} 有效而gust{cellfun(@isempty,wind)} 无效的原因仅仅是风恰好只有一个非空元素。至于真正的问题,用大括号索引单元格数组会返回索引单元格的元素;当与非标量索引(例如逻辑数组)一起使用时,您实际上是一次返回每个元素的值(您可以看到 ans 变量被覆盖了 33 次)。相反,您必须使用括号来索引数组,即返回单元格数组的单元格,并用包含您想要的单元格的单元格覆盖数组的元素 - 单元格。因此

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

      【讨论】:

      • 我刚开始注意到 ans 在您回答之前被覆盖了 33 次。好收获!
      【解决方案3】:

      大括号有所不同:

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


      摘自Cell Arrays and Their Contents

      使用卷曲 大括号 {} 用于设置或获取 元胞数组的内容。

      使用括号 () 来索引 元胞数组来收集一个子集 单元格一起在另一个单元格数组中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-17
        • 1970-01-01
        • 2014-02-16
        • 1970-01-01
        • 1970-01-01
        • 2014-04-16
        相关资源
        最近更新 更多