Effacer les filtres
Effacer les filtres

cell array to double

35 vues (au cours des 30 derniers jours)
Robert Scott
Robert Scott le 29 Juil 2021
Sorry for the elementry question
Cant seem to find a solution
I am trying to convert a cell array to double
i am using str2double(A)
where A is a 2D cell array
It is returning NAN's to me because each location is a cell array
Does anyone know how to do this?
  3 commentaires
Robert Scott
Robert Scott le 29 Juil 2021
str2double will not work
neither will cell2mat
cell2mat is saying it all has to be the same variable type
I have literally examined the cell array line by line and it is all the same type
I am really at a loss with matlab today. Some thigns are so incredibly foolish in this program
How hard does mathworks intend to make it to simply convert data sytems. 99% of my scripts that is all they day
convert data types.
dpb le 30 Juil 2021
Modifié(e) : dpb le 30 Juil 2021
Again, we cannot begin to supply specific answers for generic Q? of things we cannot see or touch...
As @Dave B notes below, since a cell can contain anything, what's in it depends on what it was that got put into it to begin with and also how that was stored -- if you ended up storing a cell inside a cell, then you've got to have a way to dereference the cell content inside the top-level cell besides...
In short, it all boils down to how this cell array was created -- and the better solution is probably going to be to look at how that was done.
Since you say mostly you're converting from one system to another, then the place to start is to look at what those systems are producing and finding the best way to bring them together -- just letting MATLAB try to figure it out on its own may run, but if have different systems it's likely to end up with disparate data forms.
No doubt folks here can help, but not without the necessary information on what it is you're trying to deal with.
If you're only interested in the specific problem at the moment, attach the variable saved as a .mat file so folks can poke at it and see just what is inside it. As a black box, nothing anybody here can do but guess.

Connectez-vous pour commenter.

Réponses (2)

Dave B
Dave B le 30 Juil 2021
Modifié(e) : Dave B le 30 Juil 2021
@Robert Scott - I think the key to understanding this is that anything can be in a cell:
a={'1' 2 datetime(2010,1,1) axes "one" struct('a',1,'b','2') [3;4] {'another cell' 1}};
a =
1×8 cell array
{'1'} {[2]} {[01-Jan-2010]} {1×1 Axes} {["one"]} {1×1 struct} {2×1 double} {1×2 cell}
Some of these things can be converted to double with one tool: like the '1' could use str2double, but the [2] couldn't because it's not stringlike so str2double wouldn't make sense...and the datetime you might convert to double with year, or datenum, depending on what you wanted. The struct and other cell would be really tricky, and cells can contain all sorts of shapes which also confuses things (like that [3;4], what would that mean for the shape of the output?).
I guess what I'm saying is: really just about anything can go into a cell, which makes them very powerful but can also make them difficult.
So the important bit of information to answer this question is what's in the cells...?
Using cellfun as @dpb suggests, might help. Think of cellfun as a tool that will run a function on each cell (but note that if the outputs are different sizes, you need to set a UniformOutput flag to false, and that means it'll return a cell which will probably make you really frustrated!)
Here's a function that would convert numeric, char, and string:
function dbl=myconverter(input)
if isnumeric(input) && isscalar(input)
elseif isstring(input) && isscalar(input)
elseif ischar(input)
error("Not sure how to convert " + class(input))
And here's using cellfun with it:
>> b={1 uint8(2) "3" '4'}
b =
1×4 cell array
{[1]} {[2]} {["3"]} {'4'}
>> cellfun(@myconverter,b)
ans =
1 2 3 4
But note it'll error if you give it something it's not equipped to handle:
>> c={[10 20] [30 40]}
c =
1×2 cell array
{1×2 double} {1×2 double}
>> cellfun(@myconverter,c)
Error using myconverter (line 7)
Not sure how to convert double with length 2
>> d={'3' hours(2)}
d =
1×2 cell array
{'3'} {[2 hr]}
>> cellfun(@myconverter,d)
Error using myconverter (line 7)
Not sure how to convert duration with length 1

Mohit Kumar
Mohit Kumar le 26 Sep 2023
You will only able to convert cell array to double in the following manner:
(1) When you import your data, follow the step shown in the figure. Select table or numeric vector..your imported data will be in double format


En savoir plus sur Data Type Conversion dans Help Center et File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by