0
votes

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!

3

3 Answers

1
votes

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
1
votes

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
1
votes

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.