MATLAB: Does isfield return with false when the field does exist in a structure

dynamically named variablesevalisfieldstructures

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

Best Answer

Avoid creating dynamically named variables using eval in MATLAB. This is poor practice as has been explained many times on this forum, and is not recommended by MATLAB themselves:
When you are a beginner it seems like a cunning and fast way to store information, but actually it is really bad practice to name your variables dynamically. MATLAB is also not intended for this kind of variable naming: if you continue to include data in the variable names then you will find yourself fighting many more of these battles against MATLAB.
However when you use more appropriate storage for your data (and meta-data) then you will suddenly find lots of MATLAB functions that do many useful operations for you, quickly and easily.
In your case a much more robust solution would be to use structures , where you can include fields for each kind of data (e.g. Process type, Flow data, Temperature data, Notes, Units, etc), or cell arrays . There are many functions that support working on structures and cell arrays, and can access these data easily, and they can also be used in vectorized code (which is something you need to learn about). Structures can be non-scalar , and you can even define structure fieldnames dynamically.
Placing your data in a structure or cell array also makes it much easier to pass to functions: can you imagine the fight you would have trying to pass hundreds of dynamically named variables to a function?
If you have a newer version of matlab you can also use a table , which stores the data together in one array but also allows key-name access to the columns. This might be a good alternative for your data.
In case you are interested, here are some pages explaining why dynamically assigning variable names is a really bad idea in MATLAB: