how do I find the type of a value inside a cell array?

99 Ansichten (letzte 30 Tage)
ilona
ilona am 28 Dez. 2013
Bearbeitet: Image Analyst am 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)

Akzeptierte Antwort

Image Analyst
Image Analyst am 28 Dez. 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 Kommentare
ilona
ilona am 28 Dez. 2013
Bearbeitet: ilona am 28 Dez. 2013
I looked at your suggestion - it is still not what I need cause I don't want to use a loop and if I want to know which one to put inside the xlsread function I have to use one
Image Analyst
Image Analyst am 28 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jan
Jan am 28 Dez. 2013
Bearbeitet: Jan am 28 Dez. 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 Kommentare
Jesús Julián de Niz Hernández
@Peter Perkins hi, why did you use the @ before the ischar? thanks
Image Analyst
Image Analyst am 12 Feb. 2021
Bearbeitet: Image Analyst am 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?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Identification finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by