有时拉出特定列,数组为空

Pulling out specific column when sometimes, the array is empty

我正在尝试从单元格中拉出一列。但是,有时单元格是空的。

例如,如果在这一行中,我尝试拉出 PM25_win{i} 中的最后一列,它有时会有一个大小为 nx28 的数组。但是,有时数组为零。

for i = 1:length(years)-1
    PM25 = table2array(PM25_win{i}(:,end));
end

当数组为空时,代码停止并且出现错误

Subscript indices must either be real positive integers or logicals. 

如果 PM25_win{i} 为空,我如何解释这两种情况,以便代码简单地将 PM25 变量创建为空数组?

您可以简单地在 for 循环中添加一个 if-else 语句。

for i = 1:length(years)-1
    if isempty(PM25_win{i}(:,end))
      PM25 = [];
    else
      PM25 = table2array(PM25_win{i}(:,end));
    end
end