100 views (last 30 days)
Show older comments
Wouter Wizard on 7 Nov 2019
Commented: Guillaume on 8 Nov 2019
Accepted Answer: CAM
Open in MATLAB Online
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 Comments Show 5 older commentsHide 5 older comments
Show 5 older commentsHide 5 older comments
the cyclist on 7 Nov 2019
Direct link to this comment
https://connections.mathworks.com/matlabcentral/answers/489754-cell-array-data-to-double-conversion#comment_764711
Can you upload the data (or a small subset) in a *.mat file, using the paper clip icon?
Stephen23 on 7 Nov 2019
Direct link to this comment
https://connections.mathworks.com/matlabcentral/answers/489754-cell-array-data-to-double-conversion#comment_764713
Edited: Stephen23 on 7 Nov 2019
"How can I transfer both numeric and non-numeric data from cel arrays to double arrays?"
How do you expect non-numeric data to be represented in numeric arrays?
Wouter Wizard on 7 Nov 2019
Direct link to this comment
https://connections.mathworks.com/matlabcentral/answers/489754-cell-array-data-to-double-conversion#comment_764719
- Assignment2.m
@ the cyclist I have added the data here
@ Stephen Cobeldick
I think that would not be logical. But the first column of my cell array has names of countries. I would like to include these in the net double array. Is there a way to do this?
Stephen23 on 7 Nov 2019
Direct link to this comment
https://connections.mathworks.com/matlabcentral/answers/489754-cell-array-data-to-double-conversion#comment_764767
Edited: Stephen23 on 7 Nov 2019
"But the first column of my cell array has names of countries."
Use a table. A table is probably the best way to store data with a header.
https://www.mathworks.com/help/matlab/matlab_prog/create-a-table.html
"I have added the data here"
You uploaded some code, but no data.
Wouter Wizard on 7 Nov 2019
Direct link to this comment
https://connections.mathworks.com/matlabcentral/answers/489754-cell-array-data-to-double-conversion#comment_764797
Okay thanks Stephen, I wil try the table!
The Assignment2.m is in my previous comment. Sorry if this caused confusion, I am new and a bit unfamiliar with how to post questions and data.
Wouter Wizard on 7 Nov 2019
Direct link to this comment
https://connections.mathworks.com/matlabcentral/answers/489754-cell-array-data-to-double-conversion#comment_764800
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 on 8 Nov 2019
Direct link to this comment
https://connections.mathworks.com/matlabcentral/answers/489754-cell-array-data-to-double-conversion#comment_765039
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).
Sign in to comment.
Sign in to answer this question.
Accepted Answer
CAM on 7 Nov 2019
Open in MATLAB Online
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 Comments Show 1 older commentHide 1 older comment
Show 1 older commentHide 1 older comment
Stephen23 on 7 Nov 2019
Direct link to this comment
https://connections.mathworks.com/matlabcentral/answers/489754-cell-array-data-to-double-conversion#comment_764716
Open in MATLAB Online
Note that
cellfun(@str2double, Comp_all)
will only run without error if each cell returns a scalar, in which case it is exactly equivalent to calling str2double directly on the cell array (just more complicated).
Possibly you forgot 'UniformOutput',false.
Wouter Wizard on 7 Nov 2019
Direct link to this comment
https://connections.mathworks.com/matlabcentral/answers/489754-cell-array-data-to-double-conversion#comment_764717
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 on 8 Nov 2019
Direct link to this comment
https://connections.mathworks.com/matlabcentral/answers/489754-cell-array-data-to-double-conversion#comment_765031
Open in MATLAB Online
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);
Sign in to comment.
More Answers (0)
Sign in to answer this question.
See Also
Categories
MATLABLanguage FundamentalsData TypesCell Arrays
Find more on Cell Arrays in Help Center and File Exchange
Tags
- cell-array
- type-conversion
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office