cell2mat for column of strings

3 ビュー (過去 30 日間)
Carmelo Gonzales
Carmelo Gonzales 2017 年 7 月 7 日
コメント済み: Stephen23 2017 年 7 月 7 日
Hello,
I am having trouble converting a cell with numbers in string format to a column vector. The cell is as follows
data={'10';'1';'40';'5'}
I would like to convert this to a vector
new_data=[10;1;40;5]
When I try to use cell2mat(cell), this returns an error: Dimensions of matrices being concatenated are not consistent.
The only work around I have found thus far is the following
for ii=1:length(data)
new_data(ii,1)=(str2num(data{ii}));
end
Where new_cell is the desired vector. This would not be a problem to use, but the real cells contain many hundreds of numbers in string format, and the loop is eating too much time.
Is there any other workaround for this?
Thanks, Carmelo

採用された回答

KSSV
KSSV 2017 年 7 月 7 日
編集済み: KSSV 2017 年 7 月 7 日
data={'10';'1';'40';'5'} ;
iwant = cellfun(@str2num,data);
The above cellfun uses loop inside. You may check with this also:
str2double(data)
  2 件のコメント
Walter Roberson
Walter Roberson 2017 年 7 月 7 日
str2double(data) should do a fine job in one step, and is more robust. For example if the user entered 'delete(''*.*'')' then str2num() would proceed to delete all of your files but str2double would detect that the string is not numeric.
Carmelo Gonzales
Carmelo Gonzales 2017 年 7 月 7 日
Thanks for the quick help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by