MATLAB Answers


Error using when cell2mat (with string characters)

Asked by Thomas Côté on 11 Jun 2019
Latest activity Edited by Thomas Côté on 11 Jun 2019
Hi, I have the following cell array that I would like to convert into a matrix with cell2mat() :
Matrix =
7×1 cell array
{'Longueur d''onde' }
{'Position 0.txt' }
{'Position_4200.txt' }
{'Position_9100.txt' }
However, I get the following error :
Error using cat
Dimensions of arrays being concatenated are not consistent.
Error in cell2mat (line 83)
m{n} = cat(1,c{:,n});
It surely has something to do with the string characters, but I'm clueless at what I should do.
Thanks for your help!


There is some confusion in this question, mainly the loose terminology about data types.
So, to clarify ...
A "matrix" is a two-dimensional array, and can only hold numeric values -- not characters, text, strings, etc.
A character array and a string (or an array of strings) are not the same thing, although both of those data types can hold text.
Cell arrays are yet another data type. They are often used as a container for different data types.
(Admittedly, it can be confusing.)
So, your variable that is named Matrix is a cell array, where each cell contains a character array. Your variable Matrix is not a matrix, and its contents cannot be converted into a matrix, because it has non-numeric data.
So, maybe you can add a comment htere, and clarify what data type you want the output to be, and what the contents should look like.
I hope that helps.
Thanks for your help!
Indeed, I shouldn't have used Matrix as the name of the cell array.
However, you answered my underlying question to all of that, which was what kind of data can we put into a MAT type and that's only numeric values. I'll just have to proceed differently.
(edited) Silly me, i wanted to play with the characters inside the "Matrix" as if they were numeric values, but I'll have to use structures or strings instead.
Have a great day!

Sign in to comment.

1 Answer

Answer by Matt J
on 11 Jun 2019
Edited by Matt J
on 11 Jun 2019
 Accepted Answer



Sign in to comment.