Converting Cell to Double

12 ビュー (過去 30 日間)
Hossein
Hossein 2014 年 5 月 5 日
編集済み: Hossein 2014 年 5 月 5 日
Hi everybody! i have a matrix named as data having n X 1 cell-elements, which comes from a text file. I am trying to convert its numeric elements to double type in order to export it as a excel file.
e.g.
data(3,1) = '0.0500 0.1236 36.00 0.00 1.00 -0.0000 0.0000 0.3337 30 30'
Its aimed to save any single value(for example 0.0500) seperately in a single array in excel output. Any suggestion? tnx

採用された回答

Justin
Justin 2014 年 5 月 5 日
編集済み: Justin 2014 年 5 月 5 日
There may be a better way to import the text file so the data is automatically read in as numeric values. Try using the Import Data button on the Home menu and see if you can change the settings for the data to be read in how you would like.
Post a couple lines of text file and I may be able to identify the best way to read it in.
If that doesn't work this is a way to reformat the data.
You could use regexp to split the string or the function strsplit. This will give you a cell array of the values you can then to convert to a numeric array after removing and empty values.
% split the string
sepVals = regexp(data{3, 1}, ' ', 'split');
% remove empties
sepVals = sepVals(~cellfun(@isempty, sepVals ));
% convert to numbers
numbers = str2double(sepVals);
You can then use xlswrite to write the array numbers to an excel sheet.
  3 件のコメント
Justin
Justin 2014 年 5 月 5 日
Is your data variable a cell array of strings?
Perhaps try using data(3, 1) or data{3, 1}? What is in the sepVals variable?
Hossein
Hossein 2014 年 5 月 5 日
編集済み: Hossein 2014 年 5 月 5 日
yes! changing into data{3, 1} answered...tnx a lot!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by