【问题标题】:How to unnest a nested cell array?如何取消嵌套嵌套的单元格数组?
【发布时间】:2015-05-08 09:04:54
【问题描述】:

您好,我正在尝试创建一个看起来像这样的元胞数组。

'3/7/2014'  '209.167.128.156'   1037 
'3/13/2014' '204.205.57.137'    8
'3/18/2014' '209.167.128.156'   164
'3/27/2014' '216.178.43.209'    825

但问题是当我运行我的代码时,我得到一个像这样的元胞数组

{1x3cell} {1x3 cell} {1x3 cell} {1x3 cell} {1x3 cell]

这是我的代码:

cell = {};
month = 'March';
[num text raw] = xlsread(sheet);
text(1,:)= [];
fanta = text(:,1);
[row col] = size(fanta);
a = 1;
for i = 1:row
    coke = fanta{i};
    [first rest] = strtok(coke, '/');
    if strcmp(first, '1') && strcmp(month, 'January')
        cell{i} = raw((a+i), :);
    end
    if strcmp(first, '2') && strcmp(month, 'February')
        cell{i} = raw((a+i), :);
    end
    if strcmp(first, '3') && strcmp(month, 'March')
        cell{i} = raw((a+i), :);
    end
    if strcmp(first, '4') && strcmp(month, 'April')
        cell{i} = raw((a+i), :);
    end
    if strcmp(first, '5') && strcmp(month, 'May')
        cell{i} = raw((a+i), :);
    end
    if strcmp(first, '6') && strcmp(month, 'June')
        cell{i} = raw((a+i), :);
    end
    if strcmp(first, '7') && strcmp(month, 'July')
        cell{i} = raw((a+i), :);
    end
    if strcmp(first, '8') && strcmp(month, 'August')
        cell{i} = raw((a+i), :);
    end
    if strcmp(first, '9') && strcmp(month, 'Sepetember')
        cell{i} = raw((a+i), :);
    end
    if strcmp(first, '10') && strcmp(month, 'October')
        cell{i} = raw((a+i), :);
    end
    if strcmp(first, '11') && strcmp(month, 'November')
        cell{i} = raw((a+i), :);
    end
    if strcmp(first, '12') && strcmp(month, 'December')
        cell{i} = raw((a+i), :);
    end
end
cell = cell(~cellfun('isempty', cell))

我做错了什么,我该如何解决。 谢谢。

【问题讨论】:

  • cell = cell(...) 是非常糟糕的做法(!!):-? (cell是matlab中的一个函数)
  • 请看我的回答:它有效。

标签: arrays matlab conditional cell


【解决方案1】:

要折叠单元格数组,您可以尝试

  C = cell2mat(C); % <-- using cell as a var name is bad practice!

试一试,看看它是否有效。例如(这是八度音阶,但应该在 matlab 中工作):

C =
{
  [1,1] =
  {
    [1,1] = 3/7/2014
    [1,2] = 209.167.128.156
    [1,3] =  1037
  }
  [2,1] =
  {
    [1,1] = 3/13/2014
    [1,2] = 204.205.57.137
    [1,3] =  8
  }
  [3,1] =
  {
    [1,1] = 3/18/2014
    [1,2] = 209.167.128.156
    [1,3] =  164
  }
  [4,1] =
  {
    [1,1] = 3/27/2014
    [1,2] = 216.178.43.209
    [1,3] =  825
  }
}

cell2mat(C)
ans =
{
  [1,1] = 3/7/2014
  [2,1] = 3/13/2014
  [3,1] = 3/18/2014
  [4,1] = 3/27/2014
  [1,2] = 209.167.128.156
  [2,2] = 204.205.57.137
  [3,2] = 209.167.128.156
  [4,2] = 216.178.43.209
  [1,3] =  1037
  [2,3] =  8
  [3,3] =  164
  [4,3] =  825
}

【讨论】:

  • @rayryeng 好吧,我可能自己使用了 cell = ... 来推动它:-)
  • 糟糕!好吧,我还是给了你一个 +1 :)
【解决方案2】:

首先,您应该从不调用变量cell,因为它是内置Matlab 函数的名称。这很糟糕,你应该为此受到惩罚:-)

假设您已将所有cell 替换为C。然后,您应该最初定义C by

C = cell(0,3);

并替换你所有的

C{i} = ...

通过

C(i,:) = ...

最后一点:最后一列将由字符串而非数字组成。如果您真的想在此列中包含数字,您可能应该考虑在代码中的某处使用函数str2double

最好的,

【讨论】:

  • 感谢您的建议。我更改了您所说的所有内容,但将所有内容都打印在同一列上。你知道它为什么这样做吗?
【解决方案3】:

似乎没有任何实际错误发生,如果您使用的是单元格,那么您基本上是在创建一个数组数组。如果您访问cell{i},它将返回一个包含 3 个项目的向量,这些项目是您显示的行。如前所述,您可以使用矩阵,但矩阵无法处理不同的数据类型(在您的情况下为字符串和整数),因此我将继续使用单元格。 此外,您可以创建多维。

所以,类似于 Crazy Rat 所说的我会替换

cell{i}

通过

cell{i,:}

是的,不要一个接一个地调用你的单元格。

【讨论】:

  • 这不能回答问题。 OP 想要 unpack 单元格数组。您只是说使用单元格是合理的,这是肯定的。
  • 但是他不能解压成正则矩阵,因为他有不同的数据类型。
  • 我没说他应该把它解压成矩阵。他想将单元格阵列解压缩成另一个二维单元格阵列。如果您查看 Try Hard 的答案,那就是 OP 正在寻找的答案。
  • 不用担心。只是想指出它,以防有人决定投反对票。我不会,因为您确信 OP 在他使用单元阵列的立场上是正确的。祝你好运!
猜你喜欢
  • 2015-08-22
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-05
  • 1970-01-01
相关资源
最近更新 更多