cell array to double
23 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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?
Thanks
3 Commenti
dpb
il 30 Lug 2021
Modificato: dpb
il 30 Lug 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.
Risposte (2)
Dave B
il 30 Lug 2021
Modificato: Dave B
il 30 Lug 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)
dbl=double(input);
elseif isstring(input) && isscalar(input)
dbl=str2double(input);
elseif ischar(input)
dbl=str2double(input);
else
error("Not sure how to convert " + class(input))
end
end
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
0 Commenti
Mohit Kumar
il 26 Set 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
0 Commenti
Vedere anche
Categorie
Scopri di più su Data Type Conversion in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!