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

103 次查看(过去 30 天)
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)

采纳的回答

Image Analyst
Image Analyst 2013-12-28
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 个评论
ilona
ilona 2013-12-28
编辑:ilona 2013-12-28
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 2013-12-28
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.

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2013-12-28
编辑:Jan 2013-12-28
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 个评论
Image Analyst
Image Analyst 2021-2-12
编辑:Image Analyst 2021-2-12
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?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by