Why does isfield return with false when the field does exist in a structure?

12 views (last 30 days)
Hello,
I'm writing a script at the moment were all I would like to do is make sure that field definitely exists in a part of a structure I'm taking data from whilst running a 'for'. If it isn't I just want to tell Matlab to 'continue' and skip that part of the loop.
What I'm finding at the moment is that if I use 'isfield' to tell me if a field exists or not within a part of the structure, it keeps telling me that the field does not exist either if it does or doesn't. It consistently returns a '0' instead of a '1' in other words.
The basic code I'm using at this point is:
structure = 'Move'; % name of the structure
for p = 1:21
get_sound_types=['sound_type=fieldnames(' structure '.P' int2str(p) ');'];
eval(get_sound_types)
for sound_ind = 1:length(sound_type)
for bn = 2:4
current_struct_field = ...
[structure '.P' int2str(p) '.' sound_type{sound_ind}];
current_block = ['B' int2str(bn)];
existence = isfield(current_struct_field,current_block);
if existence == 0
continue
%.... (other stuff happens and end statements)
When I try and use this, existence variable always returns a 0, regardless of whether the particular field exists or not. I'm sure I'm doing something silly wrong but I don't know what it could be.
Any help would be great.
Cheers, Chris
  1 Comment
Stephen23
Stephen23 on 19 Mar 2015
Edited: Stephen23 on 19 Mar 2015
This code confuses strings representing evaluable code and structures: the variable current_struct_field is a simple string, so it definitely has no fields (regardless of their names). The fact that you think it is a structure is irrelevant, and is a good example of why it is a very good idea to avoid using eval for such trivial code: it obfuscates what the code is actually doing, and the current state of the variables.
If you had defined variables normally, then you would immediately be able to see that they were strings and not structures, and know that there was a problem to fix. eval hides everything from you until the code is evaluated: MATLAB's inbuilt code-checking tools don't work if you use eval, so you lose those checks and highlighting that lets you fix error like this before they drive your crazy: "why is it doing this?"
Learn to avoid using eval for trivial tasks such as defining variables. Avoiding eval means problems like this are easier to debug, and are easier to avoid in the first place.
In your case it would be many times easier to simply use:
  • a cell array of structures if the structure have different fields, or
  • a non-scalar structure if the fields are the same.
Doing this would not require any dynamic variable naming, and would avoid this confusion between strings to be evaluated and actual structures.
Read my answer to know why using eval is not recommended.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 19 Mar 2015
Edited: Stephen23 on 12 Sep 2023
  1 Comment
Chris
Chris on 19 Mar 2015
Hi Stephen,
Thanks so much for this answer. This is a huge help and a valuable lesson in how I can dynamically reference field names too. I just rejigged some of my code using some new methods mentioned in the posts you listed and my code is looking so much better.
Cheers! Chris

Sign in to comment.

More Answers (1)

James Tursa
James Tursa on 19 Mar 2015
It looks to me like the variable current_struct_field is a string, not a struct, so isfield will return a 0 since you didn't pass it a struct in the 1st argument. Do you need to use another evil eval there?

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!