Search code examples
matlabdatasetcell-array

Display Cells in table or text file with different dimensions in Matlab


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!


Solution

  • 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