how can I convert cell2mat when the values are numbers with different number of figures

Hi, I am trying to convert a 1828x1cell to a matrix so that I could make calculation with it.
The cell contains str in the form
'1'
'23'
'2'
'14'
When a try: data=cell2mat(d);
I get the following error:
_*Error using cat Dimensions of matrices being concatenated are not consistent.
Error in cell2mat (line 84)_ m{n} = cat(1,c{:,n});*
So, the problem is that in some case the number is one figure, like 1 and 2, but in other cases the number is two figures like 23 or 14.
Please, how can I solve this problem or what else can I do so that I can do some calculation with the values inside the cell?
Thanks

 採用された回答

kjetil87
kjetil87 2013 年 7 月 19 日
your problem is that the cell contains strings, and '1' and '23' does not have the same length, '23' is actually a 1x2 array of char. Therefor it cannot be placed in a matrix, it would work if u replaced '1' with eg '01'. If you are going to do calculations with these numbers i would recommand taking a look at "str2num" or "str2double".
Perhaps
data=cellfun(@str2num,d);
would solve your problem ?

6 件のコメント

Radu
Radu 2014 年 5 月 19 日
Thanks! Worked for me.
Zahra
Zahra 2014 年 10 月 21 日
Thanks, it worked!
Paul Calaoagan
Paul Calaoagan 2016 年 7 月 14 日
Thank you very much! This answer was very helpful!
data = str2double(d);
would work as well if d is a cell array of strings.
Kazi Shakib
Kazi Shakib 2018 年 10 月 27 日
But when i do this it shows NaN values not the cell values.
Stephen23
Stephen23 2018 年 10 月 27 日
編集済み: Stephen23 2018 年 10 月 27 日
@Kazi Shakib: then it seems that you tried to use str2double on a cell array of character vectors that do not represent numbers. If you want more help with this ask a new question, and upload your data in a .mat file by clicking the paperclip button.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Data Type Conversion についてさらに検索

質問済み:

2013 年 7 月 18 日

編集済み:

2018 年 10 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by