how do I find the type of a value inside a cell array?
99 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
ilona
il 28 Dic 2013
Modificato: Image Analyst
il 12 Feb 2021
I need to load only one variable from a mat file. this variable is a cell array that contains a string and a scalar. but I don't know in which order (the first place in the cell array can be a scalar or a string) how can I find which one of them is the string maybe using find? thanks
a code for example:
% code
load('Myfavoritefile.mat','myFavoriteVar')
myFavoriteVar ={'ExampleString' 5}
% or {5 'ExampleString'} I don't know but I only need the string
%the string is the name of the xl file I want to open
[NUMERIC,TXT,RAW]=xlsread(ExampleString)
0 Commenti
Risposta accettata
Image Analyst
il 28 Dic 2013
Try this:
classOfElement1 = class(myFavoriteVar{1})
classOfElement2 = class(myFavoriteVar{2})
Use strcmp() to check if each one is 'double' or 'char'
TF = strcmp(classOfElement1, 'double');
TF = strcmp(classOfElement1, 'char');
2 Commenti
Image Analyst
il 28 Dic 2013
What's wrong with a loop? Tell me, please. Anyway, all you have to do is to look at both elements in the first row of your variable to determine which is the character and which is a number. It will undoubtedly be the same for all subsequent rows. Let's say the filename was found to be in column 2, then you just have a loop over all rows getting the particular filename out of that row and reading in the workbook specified by that name.
Get over your fear of loops. You can do a loop of tens of millions of iterations in a few milliseconds. Look:
s=int32(0);
tic
% Do a million additions
for k = 1 : 1000000
s = s+1;
end
toc;
Elapsed time is 0.016758 seconds.
There, a million additions and it was only a hundredth of a second. Now, do you still have a problem with it? How many rows are in your cell array? If you have tens of millions , then you may have a case.
Più risposte (1)
Jan
il 28 Dic 2013
Modificato: Jan
il 28 Dic 2013
Simply test the type of the first cell element:
myFavoriteVar = load('Myfavoritefile.mat','myFavoriteVar')
if ischar(myFavoriteVar{1})
ExampleString = myFavoriteVar{1};
ExampleData = myFavoriteVar{2};
else
ExampleString = myFavoriteVar{2};
ExampleData = myFavoriteVar{1};
end
[NUMERIC,TXT,RAW]=xlsread(ExampleString);
8 Commenti
Jesús Julián de Niz Hernández
il 12 Feb 2021
@Peter Perkins hi, why did you use the @ before the ischar? thanks
Image Analyst
il 12 Feb 2021
Modificato: Image Analyst
il 12 Feb 2021
Julian, the @ tells it that ischar is the name of a function and not to try to evaluate (run) ischar right then and there.
Otherwise it WILL try to run it and since you don't have any argument being passed to it within parentheses, it will say that you didn't supply any arguments to ischar(), like this:
>> find(cellfun(ischar,c))
Error using ischar
Not enough input arguments.
See, it basically tried to do this
ischar()
and take the result, along with the cell array called c, and pass them to the cellfun() function as inputs. But ischar() with no arguments is not allowed, so it throws an error. Does that explain it?
Vedere anche
Categorie
Scopri di più su Data Type Identification in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!