Search within a structure using a string
4 次查看(过去 30 天)
显示 更早的评论
Hello Im trying to check if a string exist within a strucure:
The string is the structure path, which I created using sprintf I can not detect if the string coincide with the path of an existent structure in the workspace (Clicks).
Eval dosent work for structures neither fieldnames ... there is any way to check if exist or not in the workspace???
thanks in advance
prof_l=unique(prof_list);
shore_l=unique(shore_list);
% read clicks and extract shoreline
met=('fix');
%
for i1=1:numel(prof_l)
p=prof_l(i1,1);
for i2=1:1:numel(shore_l)
s=shore_l(i2,1);
file=(sprintf('Clicks.prof_%u.shore_%u.%s',p,s,met));
if exist(eval(file),var) ==1;
disp('hola')
end
end
end
0 个评论
采纳的回答
Walter Roberson
2015-9-5
Here is the test you asked for. I would point out that if the answer is True, that the structure exists, chances are high that you are planning to use eval() to get at the value stored, and that you should be strongly avoiding doing that.
function tf = struct_exists(candidate)
%Written 20150905 - Walter Roberson
%{
Here is a structure exist. In this version it *only* handles flat structures, no
indexing, no dynamic fieldnames.
The empty string and [] are accepted as valid arguments (and return false). Other
non-string arguments will generate a warning (and false)
The last field given is _not_ tested to see if it is a structure (only whether it
exists) so that this routine can be used to test whether a field exists. As a design
decision this extends to the case where the passed name has no subfields given and
so is the last field name given: in this case true is returned if the name exists
whether or not the name is a structure.
The use of warnings instead of error was a design decision, one that is most
debatable for the case of invalid input. I used warnings because this is
supposed to be an existence test, and if the answer is "No that is not a valid
arrangement of structure references" then Okay, that's why you *tested* rather
than just went ahead and used the string.
%}
tf = false;
if ~( ischar(candidate) || (isdouble(candidate) && isempty(candidate)) )
warning('struct_exist: only character strings or [] permitted as argument')
return
end
if isempty(candidate); return; end
if regexp(candidate, '[^a-zA-z0-9_.]')
warning('struct_exist: only fieldnames and ''.'' permitted');
return;
end
parts = regexp(candidate, '\.', 'split');
if ~ evalin('caller', sprintf('exist(''%s'', ''var'')', parts{1}));
warning( sprintf('struct_exist, "%s" is not a variable', parts{1}) );
return;
end
for K = 1 : length(parts) - 1
subname = strjoin(parts(1:K), '.');
try
fn = evalin('caller', sprintf('fieldnames(%s)', subname));
catch ME
%some leading fieldname was not a structure
fn = {};
end
if ~ismember(parts{K+1}, fn)
%some leading prefix does not have the next part as a field
% no warning because this is a normal use case
return
end
end
tf = true;
end
更多回答(1 个)
per isakson
2015-9-5
编辑:per isakson
2015-9-5
Try
>> S.a.b.c = 'abc';
>> S.('a').('b').c
ans =
abc
and
str = Clicks.( sprintf('prof_%u',p) ).( sprintf('shore_%u',s) ).met;
if exist( str, 'var' ) == 1
disp('hola')
end
6 个评论
per isakson
2015-9-5
编辑:per isakson
2015-9-6
"in the attemp to check a substructure inside the structure"   I don't understand what exactly you want to check. In plain English, what is your intention with
isfield( S.a.b, 'S.a' )
isfield( S, 'S.a' )
Under what circumstances should they return true and false, respectively?   'S.a' isn't a legal field name.
 
To check whether a value is a structure use isstruct
>> isstruct( S )
ans =
1
>> isstruct( S.a )
ans =
1
>> isstruct( S.a.b )
ans =
1
>> isstruct( S.a.b.c )
ans =
0
>> ischar( S.a.b.c )
ans =
1
>>
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!