Search code examples
matlabcell

MATLAB cell2mat concatenating numbers together


I'm trying to plot something in a cell array, so I'm trying to convert one of the columns to a matrix.

I pulled out the column from the cell array and tried to do cell2mat on it to turn it into a matrix. However, cell2mat seems to just turn it into one long character array.

site(:,4)'; % Pull out column 4 from the cell array
cell2mat(ans); % Attempt to convert the cell into a matrix

The first part of the code gives me: 10.4 10.1 7.9 8.2

The second part of the code gives me: 10.410.17.98.2

How can I make the cell into a matrix that I can use to plot a graph?


Solution

  • It appears that your cell array contains strings, is that correct? In that case you don't use cell2mat, but str2double:

    str2double(site(:,4).')
    

    For example:

    >> site = {'1',   '2',   '3',   '4';
               '1.1', '2.1', '3.1', '4.1'};
    >> str2double(site(:,4).')
    ans =
        4.0000    4.1000