【问题标题】:Display Cells in table or text file with different dimensions in Matlab在 Matlab 中显示不同尺寸的表格或文本文件中的单元格
【发布时间】:2014-07-09 08:23:08
【问题描述】:

我在 MATLAB R2012a 中有一个单元格数据类型,其中不同的单元格具有不同的维度,我无法在表格或文本文件中显示结果。两者都会很棒。我的手机是这样的

'detected' 'crane'   crane' 'time'
'overlap'   [99,88] [99,88]  900
'overlap'   [99,98] [99,88]  1000

... 所以问题是包含 2 个数字数组的起重机。

我想在表格中查看它并将其打印到文本文件中,但我不断收到与不正确尺寸有关的错误。


我试过了

T=cell2table(collision(2:end,:),'Variable Names', collision(1,:));

出现错误

'cell' 类型的输入的未定义函数 cell2table

例如,我也尝试将每个单元格变成一个矩阵

crane1data = cell2mat(collision(2:end,2))

然后尝试将它们全部组合,但它们的尺寸不同,1x2 和 2x2,所以我得到错误

CAT 参数维度不一致

感谢您的帮助!

【问题讨论】:

    标签: matlab dataset cell-array


    【解决方案1】:

    您可以将所有单元格转换为字符串并使用 fprintf:

     C = {
        'detected'  'crane1'  'crane2' 'time'
        'overlap'   [99,88]   [99,88]  900
        'overlap'   [99,98]   [99,88]  1000
    };
    
    [nRow nCol] = size(C);
    fID = fopen('textFile.txt','w');
    for i = 1:nRow
        for j = 1:nCol
            if ~ischar(C{i,j})
                fprintf(fID,'%10s',num2str(C{i,j}));
            else
                fprintf(fID,'%10s',C{i,j});
            end
    
            if j == nCol
                fprintf(fID,'\n');
            end
        end
    end
    fclose(fID); 
    

    textFile.txt 中的结果:

      detected    crane1    crane2      time
       overlap    99  88    99  88       900
       overlap    99  98    99  88      1000
    

    【讨论】:

      【解决方案2】:

      假设你有这个元胞数组:

      >> C = {
          'detected'  'crane1'  'crane2' 'time'
          'overlap'   [99,88]   [99,88]  900
          'overlap'   [99,98]   [99,88]  1000
      }
      C = 
          'detected'    'crane1'        'crane2'        'time'
          'overlap'     [1x2 double]    [1x2 double]    [ 900]
          'overlap'     [1x2 double]    [1x2 double]    [1000]
      

      你把它转换成 MATLAB 表格怎么样:

      >> T = cell2table(C(2:end,:), 'VariableNames',C(1,:))
      T = 
          detected      crane1      crane2     time
          _________    ________    ________    ____
          'overlap'    99    88    99    88     900
          'overlap'    99    98    99    88    1000
      

      【讨论】:

      • 我之前尝试过,但我得到了错误 undefined function cell2table for input arguments of 'cell'
      • 如果您向我们展示您尝试过的内容(使用代码)并提及您收到的任何错误(编辑您的问题并发布该信息),我会很有帮助...无论如何,MATLAB 的版本是什么你在跑步吗? table 是最近推出的,但它实际上与统计工具箱提供多年的 dataset 完全相同..
      • @AustenNovis:就像我说的,尝试改用dataset 类(假设您有统计工具箱):D = cell2dataset(C(2:end,:), 'VarNames',C(1,:))
      【解决方案3】:

      尝试以下方法:

      以问题中发布的相同示例为例,

      C = 
      
          'detected'    'crane1'        'crane2'        'time'
          'overlap'     [1x2 double]    [1x2 double]    [ 900]
          'overlap'     [1x2 double]    [1x2 double]    [1000]
      
      ds=cell2dataset(C); % This command converts the cell array to dataset assuming the first 
                          % row as the header.
      export(ds,'file','test.txt'); % This will `export` the `dataset` to the text file 
                                    % `test.txt` 
      

      这将保存具有以下结果的文本文件:

      detected    crane1_1    crane1_2    crane2_1    crane2_2    time
      overlap        99          88          99          88        900
      overlap        99          98          99          88       1000    
      

      这里crane1_1crane1_2对应crane1的两个子列。 希望对您有所帮助。

      【讨论】:

      • 原来我的 Matlab 版本没有 cell2dataset 或 cell2table。它只有 cell2mat 和 cell2struct。我想这就是我遇到这么多问题的原因
      猜你喜欢
      • 1970-01-01
      • 2016-10-07
      • 2020-02-09
      • 1970-01-01
      • 2014-09-03
      • 2012-06-11
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      相关资源
      最近更新 更多