Error using when cell2mat (with string characters)

41 次查看(过去 30 天)
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_12500.txt'}
{'Position_15015.txt'}
{'Position_19000.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!
  2 个评论
the cyclist
the cyclist 2019-6-11
编辑:the cyclist 2019-6-11
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.
Thomas Côté
Thomas Côté 2019-6-11
编辑:Thomas Côté 2019-6-11
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!

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2019-6-11
编辑:Matt J 2019-6-11
Try
char(Matrix)
or
string(Matrix)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品


版本

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by