Looping through different structures?

6 views (last 30 days)
Hi,
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);
end
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!
  3 Comments
Jonathan
Jonathan on 26 Aug 2022
Thank you for taking the time to respond, but unfortunately I'm not understanding how the information in that tutorial can help me. It was not my choice to sequentially name the structures Ray_1, Ray_2, Ray_3 etc., this is how the data is formatted by the OpticStudio software when exporting to .mat format, so I'm stuck with that as my starting point.
I've tried various approaches and I'm still struggling to find a way to check each Ray Structure individually. For example, I tried loading each structure in one by one:
struct_name = append('Ray_',num2str(1));
Data = load('Input.mat', 'Ray_1');
But then Data is a 1x1 struct with a field 'Ray_1', itself a structure. If I try to use struct_name to access the desired fields (hit_object, or path_to) I get the error:
hits = Data.name.hit_objects
Unrecognized field name "struct_name".
I've tried loading in just the structure field:
Data = load('Input.mat', 'Ray_1.hit_object');
But get the error:
Warning: Variable 'Ray_1.hit_object' not found.
Tried other options but always seem to hit the same issue, at some point I need to define the name of the first/next Ray structure I want to check, but Matlab treats this name as a string rather than a structure name.

Sign in to comment.

Accepted Answer

Stephen23
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
D.hit_object
end
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:
  2 Comments
Jonathan
Jonathan on 26 Aug 2022
"You did not tell us if those structures are all saved in one MAT file"
My apologies, I thought that was implicit in my description.
I managed to get something working using eval, though as I understand this is not best practice I will try your alternative suggestions. Thanks for your help.
Stephen23
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)

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!