Why is cell2mat returning a char array and not an ordinary array?

8 vues (au cours des 30 derniers jours)
Kim
Kim le 31 Juil 2015
Commenté : Kim le 31 Juil 2015
In the documentation it says cell2mat returns an ordinary array, but when I use it I get a n x n char array. Does anyone know how to make it do what it says it should do?
Thanks

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 31 Juil 2015
Kim what is an ordinary array? array of string doesn't exist in Matlab.
s={'ab' 'cd';'er','mh'}
cell2mat(s)
Result
ans =
abcd
ermh
If the cells are different sizes, cell2mat doesn't work
If you have a cell of numbers n={12 145;62 78}. There is no problem
n={12 145;62 78}
cell2mat(n)
  1 commentaire
Kim
Kim le 31 Juil 2015
Thanks for your quick response! I guess I mean by ordinary something like a simple numerical matrix. I am new to matlab so I apologize for wrong vocabulary. I started with a cell array as a result of inputdlg. I input numbers and I could see them in the cell, but when I used cell2mat I got a char array.

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 31 Juil 2015
Because character arrays are what's inside your cells. See the FAQ: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
To use cell2mat with arrays, they have to be the same size, as Azzi said. If they're different sizes, you can use char() and it will create a rectangular array and pad it with spaces.
s={'two' 'abcd'; 'three','lotsOfChars'}
sArray = char(s)
  1 commentaire
Kim
Kim le 31 Juil 2015
Thank you for your help! I am fairly new to Matlab.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by