0

I have a cell data type in MATLAB R2012a with different cells having different dimensions and I am having trouble displaying the results either in a table or in a text file. Both would be great. My cell looks like this

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

... so the problem is the crane which contains an array of 2 numbers.

I want to view this in a table and print it to a text file but I keep getting errors having to do with the incorrect dimensions.


I have tried

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

and got error

undefined function cell2table for input of type 'cell'

I also tried making each cell into a matrix for example

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

and then tried to combine them all, but they are different sizes 1x2 and 2x2 so I get error

CAT argument dimensions are not consistent

Thanks for your help!

Amro
  • 123,847
  • 25
  • 243
  • 454
Austen Novis
  • 444
  • 1
  • 12
  • 30

3 Answers3

1

Say you had this cell array:

>> 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]

How about you convert it into a MATLAB table:

>> 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
Amro
  • 123,847
  • 25
  • 243
  • 454
  • I tried that earlier and I got the error undefined function cell2table for input arguments of type 'cell' – Austen Novis Jul 09 '14 at 08:42
  • I would be helpful if you show us what you have tried (with code) and mention any errors you've received (edit your question and post that info)... Anyway what version of MATLAB are you running? `table` was recently introduced, but it's really the same thing as a `dataset` which was provided by the Statistics toolbox for many years now.. – Amro Jul 09 '14 at 08:47
  • @AustenNovis: like I said, try using the `dataset` class instead (assuming you have the Statistics Toolbox): `D = cell2dataset(C(2:end,:), 'VarNames',C(1,:))`. – Amro Jul 09 '14 at 09:47
1

You can convert all the cells to strings and use 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); 

The result in textFile.txt:

  detected    crane1    crane2      time
   overlap    99  88    99  88       900
   overlap    99  98    99  88      1000
Pablo EM
  • 6,190
  • 3
  • 29
  • 37
1

try the following:

Take the same example posted in the question,

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` 

This will save the text file with the following result:

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

Here crane1_1 and crane1_2 correspond to the two subcolumns of crane1. Hope this helps you.

Naveen
  • 306
  • 1
  • 6
  • Turns out my version of Matlab doesnt have cell2dataset or cell2table. It just has cell2mat and cell2struct. I guess that is why I was having so many problems – Austen Novis Jul 09 '14 at 09:55