Nishant
Nishant

Reputation: 133

Matlab cell2mat error: all contents of the input cell array must be of the same data type

I'm trying to apply the cell2mat function to a cell consisting of strings that are either empty or are integers. I get this error telling me that the contents of the cell are not all of the same data type, even though when I ran "iscellstr" on an index with an empty string and an index with an integer sting, both returned 1, for true. What else could be causing this error?

Upvotes: 3

Views: 8291

Answers (1)

Rafael Monteiro
Rafael Monteiro

Reputation: 4549

Try using str2double instead of cell2mat.

You probably have empty cells [] instead of empty strings ''.

Example:

>> M = {'123'; ''; []; '-2'}

M = 

    '123'
    ''
    []
    '-2'

Notice there are empty cells and empty strings. cell2mat raises this error:

>> cell2mat(M)
Error using cell2mat (line 46)
All contents of the input cell array must be of the same data type.

But str2double returns this:

>> str2double(M)

ans =

   123
   NaN
   NaN
    -2

Upvotes: 2

Related Questions