Search within a structure using a string
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (1)
per isakson
el 5 de Sept. de 2015
Editada: per isakson
el 5 de Sept. de 2015
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 comentarios
Jules Ray
el 5 de Sept. de 2015
per isakson
el 5 de Sept. de 2015
Editada: per isakson
el 5 de Sept. de 2015
"Your methods give an error with exist that is not designed for structures"   I think my method is sound! My problem is that I have to backward engineer your code.
I assume that the value of Clicks.prof1.shore2.met is a string. (Note that I just picked 1 and 2.) That's why I assigned the value to a variable called str. Next, I assumed that you want to test whether there exists a variable, the name of which is the value of str.
Your "try-catch-end" construct tests whether the fields exist. That is best done with the function isfield.
Please don't write "give an error" without further detail. It's just not helpful.
Jules Ray
el 5 de Sept. de 2015
per isakson
el 5 de Sept. de 2015
Editada: per isakson
el 5 de Sept. de 2015
"I think "exist" fails"   See exist, Check existence of variable, function, folder, or class. That's right, exist cannot test for existence of fields in a structure. There is a special function for that, isfield
>> S.a.b.c = 'abc';
>> isfield( S.a.b, 'c' )
ans =
1
>> isfield( S.a.b, 'd' )
ans =
0
>> isfield( S, 'a' )
ans =
1
>> isfield( S, 'z' )
ans =
0
Note that the values of S.a.b and S are structures.
Jules Ray
el 5 de Sept. de 2015
Editada: per isakson
el 6 de Sept. de 2015
per isakson
el 5 de Sept. de 2015
Editada: per isakson
el 6 de Sept. de 2015
"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
>>
Categorías
Más información sobre Variables en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!