Looping through different structures?
Show older comments
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
"The information for each ray is saved in a separate structure, named sequentially (Ray_1, Ray_2 etc.)"
Jonathan
on 26 Aug 2022
"unfortunately I'm not understanding how the information in that tutorial can help me."
Accepted Answer
More Answers (0)
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!