Cell array data to double conversion

341 ビュー (過去 30 日間)
Wouter Wizard
Wouter Wizard 2019 年 11 月 7 日
コメント済み: Guillaume 2019 年 11 月 8 日
Hi all,
I have a cell array that I want to transform to a double array. Then I would like to fill in the double array with data from cell arrays. To solve this problem I have tried two things:
  • I have tried cell2mat, which gave a result that I couldent interpret and didnt work.
  • I tried str2double, this does not give an error, but it writes only NaN values in the double array.
The names of the variables are
  • Comp_all: cell array
  • dat: translate cell to double
  • dat2013: double array which I would like to fill in
How can I transfer both numeric and non-numeric data from cel arrays to double arrays?
The code is used to do this looks like:
dat = str2double(Com_all);
dat2013 = zeros(length(dat),5);
dat2013(1:end,1) = dat(1:end,1);
  7 件のコメント
Wouter Wizard
Wouter Wizard 2019 年 11 月 7 日
Another problem that I face is that I have the right information filtered in one of the cell arrays. I want to use that array as index for other cell arrays to extract the right data from it. However, I get an error that I cannot use a cell array for indexing.
To create input for the table I need the extracted data from the cell arrays. But I cannot translate the cell arrays into double arrays which allow indexing.
Guillaume
Guillaume 2019 年 11 月 8 日
Stephen's advice: "Use a table. A table is probably the best way to [work with your type of data"
Note that I already gave you that advice (and plenty more...) several days ago in a question that you then deleted (so I completely wasted my time helping you).

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

採用された回答

CAM
CAM 2019 年 11 月 7 日
Assuming each cell has only one entry, try using cellfun with str2double to convert the cell array of strings to a cell array of doubles, then use cell2mat.
Air code (untested; may need adjustment):
dblComp_all = cellfun(@str2double, Comp_all);
dat = cell2mat(dblComp_all);
  3 件のコメント
Wouter Wizard
Wouter Wizard 2019 年 11 月 7 日
Thank you for replying CAM7, and providing a new code that I can test.
I tried to run the code and it gave this error:
Error in cell2mat (line 42)
cellclass = class(c{1});
Error in Assignment (line 55)
dat = cell2mat(dblCom_all);
Wouter Wizard
Wouter Wizard 2019 年 11 月 8 日
With a fresh look and some modifications in my data your answer results in the correct solution. The lines that work for me are:
data = cellfun(@(s) strrep(s, ' ', ''), data, 'UniformOutput', false);
dbldata = cellfun(@str2double, data);

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by