Looping through different structures?

1 view (last 30 days)
I am trying to analyse simulated rays traced from Zemax OpticStudio. The information for each ray is saved in a separate structure, named sequentially (Ray_1, Ray_2 etc.). Each structure contains several 1-by-"n" fields, those of interest to me are hit_object and path_to.
What I want to is analyse each ray, check the field hit_object to see which entry in that field contains a certain value, and look up the field path_to to find the corresponding ray path length.
What I'm able to do is copy the field hit_object for a specified ray into a variable (let's call it hits):
hits = vertcat(Ray_1.hit_object);
Then I can search this to find the index where hits is equal to a particular value (12 in this example):
idx = find(hits == 12)
Similarly, I can copy path_to into a variable (path) and obtain the desired value:
path = vertcat(Ray_1.hit_object)
value = path(idx)
Next I want to do this but checking each structure in turn (Ray_1, then Ray_2, Ray_3 etc.). I tried using this for loop:
for ray_num = 1:1:10
Ray2Read = append("Ray_",num2str(ray_num));
hits = vertcat(Ray2Read.hit_object);
idx = find(hits == 12);
path = vertcat(Ray2Read.hit_object);
value(ray_num,:) = path(idx);
However I get the error on the third line:
Unrecognized method, property, or field 'hit_object' for class 'string'.
Obviously Matlab is treating Ray2Read as a string, rather than treating the string as the name of the structure I'm referencing.
How could I write some code to check each ray structure?
I'm also sure that the way I'm finding the desired value isn't the most efficient, so any pointers on that too would be great!

Sign in to comment.

Accepted Answer

Stephen23 on 26 Aug 2022
Edited: Stephen23 on 26 Aug 2022
You did not tell us if those structures are all saved in one MAT file, or each separately in their own MAT files. That would change the details of how you could easily handle the importing to avoid the problem you are facing.
If all of the structures are in one MAT file then LOAD it into an output variable (like you always should):
S = load(..); % all structures as fields of one scalar structure
You can then get a list of all of the structures using FIELDNAMES and trivially loop over them:
F = fieldnames(S);
for k = 1:numel(F)
D = S.(F{k}) % D is one structure
Note the syntax for accessing any one field using a dynamic fieldname (this seems to answer your question):
If each structure is saved in its own MAT file then you would need to import them into one array: a simple trick is to use STRUCT2CELL on the output of LOAD, then simply access the cell content (without needing to know the fieldname), and assign as normal to some container array:
Stephen23 on 26 Aug 2022
"I managed to get something working using eval..."
using EVAL to access structure fields has been deprecated for nearly twenty years:

Sign in to comment.

More Answers (0)


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!