how to search values?
19 次查看(过去 30 天)
显示 更早的评论
I was wondering if there is a way to find if any value of all my variables have a specific value e.g. NaN or Inf.Using OOP technics, workspace dont show all the variables,so i have to search manually all the variables in all classes to check it. Thanks in advance
2 个评论
回答(2 个)
Andrew Reibold
2014-9-26
编辑:Andrew Reibold
2014-9-26
To list all variables in your workspace
variables = who;
With this, you can loop through every variable, and check if it has a nan value
for i = 1:length(variables)
...
%check for inf and nan and whatever else you want
...
end
. FYI - If you didn't know, you can use eval('string') to check the values of the strings of variable names .
Here is an example of one way to check for infs and nans. I'm not sure how exactly you want to do it. I hope this helps!
MyArray = [ 0 1 4 3 nan 3 4 nan 3 inf ];
find(isnan(MyArray))
ans =
5 8
find(isinf(MyArray))
ans=
10
0 个评论
Guillaume
2014-9-26
To check if any of the values of cell arrays, structures or objects are NaN or Inf, you would have to write a function that inspect the cell array elements / structure fields / object properties, and call itself again if any of them is itself a cell array / structure / object.
Something like this (untested, there may be errors):
function [tf, names] = isnanobject(o)
names = {};
if isnumeric(o) || ischar(o)
tf = any(isnan(o));
elseif iscell(o)
tf = any(cellfun(@isnan, o(:)));
elseif isstruct(o)
tf = false;
for se = 1:numel(o) %in case o is an array of structure
for fn = fieldnames(o)'
[tff, namef] = isnanobject(o(se).(fn))
if tff
tf = true;
names = [names, sprintf('(%d).%s%s', se, fn, namef)];
end
end
end
elseif isobject(o)
tf = false;
for so = 1:numel(o) %in case o is an array of objects
for pn = properties(o)'
[tff, namep] = isnanobject(o(se).(pn))
if tff
tf = true;
names = [names, sprintf('(%d).%s%s', se, fn, namep)];
end
end
end
else %neither numeric, char, cell, struct, or object
error('forgot something');
end
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!