Solved

Indexing Objects in the NCE (from Matlab)

  • 17 January 2022
  • 5 replies
  • 239 views

Userlevel 2
Badge

If I have say, five more or less identical objects in the NCE, that are in different X,Y positions, and I’m accessing the NCE via an Interactive Extension, is there a way to index the assigned names of the NCE rows somehow?

 

For example, in the Matlab extension

instead of having to create five unique Objects: Heliostat1, Heliostat2,… Heliostat5, as shown below, 

Heliostat1 = TheNCE.GetObjectAt(2);

Heliostat2 = TheNCE.GetObjectAt(3);

Heliostat3 = TheNCE.GetObjectAt(4);

Heliostat4 = TheNCE.GetObjectAt(5);

Heliostat5 = TheNCE.GetObjectAt(6);

is there a way to index the left side variable names so that they can be used in a loop? Such as: 

for h = 1:5

Heliostat(1) = TheNCE.GetObjectAt(h+1);

end;

When I try the above, I get something like: “Array formation and indexing are not allowed on .NET objects.” I’m hoping there is some sensible way to do this. 

 

This would be extremely helpful when performing looped calculations to alter the physical properties of each of these objects in the NCE and then wanting to set these properties back inside OpticsStudio before a Ray Trace. Especially with larger numbers of objects. 

The 3D Layout visualization in OpticsStudio makes troubleshooting the calculations sooo much easier and I’m not really willing to give that up. 

Open to suggestions. 

icon

Best answer by David.Nguyen 17 January 2022, 12:46

View original

5 replies

Userlevel 7
Badge +2

Hi Josh,

 

I’m affraid I haven’t programmed with MATLAB in a long time, and I do not own a licence at the moment. But, this definitely sounds like something MATLAB should be able to do. I noticed a couple of odd things in your code.

First, in your loop, you always write Heliostat(1), meaning in every iteration you will just override the value of Heliostat(1) by the latest object. Second, you use the method GetObjectAt(h+1). However, if h is from 1 to 5, h+1 is from 2 to 6. If your system doesn’t have a line 6 (remember objects start from 1 in OS as well) it will probably throw an error.

I don’t know if this helps, but I’ve made the Python equivalent for your to check:

 

TheNCE = TheSystem.NCE

number_of_objects = TheNCE.NumberOfObjects

ns_objects_list = []

# Populate the <ns_objects_list> variable with the non-sequential objects
for ns_object_index in range(1, number_of_objects+1):
    ns_objects_list.append(TheNCE.GetObjectAt(ns_object_index))
    
# Verify by printing the comment cell of each object
for ns_object in ns_objects_list:
    print(ns_object.Comment)

 

I hope the code is self-explanatory. If my NCE looks like below:

Then the code returns:

Let me know if this is what you wanted and whether you were able to fix your issue.

Take care,

 

David

Userlevel 2
Badge

Thanks David,

that helps! Matlab has a peculiarity/deficiency that it doesn’t allow arrays or indexing with .NET objects. If I skip importing the objects as a whole, and just read and write the 6 or so individual object parameters that are needed (x, y, z positions and tilts), then the problem goes away since I can index the h in “GetObjectAt(h).parameter”.

Good catch on the “Heliostat(1)” above. That should be “Heliostat(h)”. I had just put something simple down to illustrate the problem, rather than my actual code. 

And h+1 is correct since the Object #1 in OpticsStudio is the Sun / Source and the following objects are the heliostats. 

Thanks again for the help getting me over the obstacle!

Josh

Userlevel 7
Badge +2

Hi Josh,

 

I’m glad it helps. This limitation seems important to know for MATLAB users, I definitely didn’t know about it. I guess you could make your own MATLAB import function that would instantiate a custom MATLAB object (I don’t remember if there are classes in MATLAB).

I see, my intuition that it could have been an array index issue was wrong then. Fortunately, it seems like you have a workaround for now. Good job.

Take care,

 

David

Userlevel 7
Badge +3

Hi Josh,

In Matlab you can index surfaces and/or objects by using a cell array.  Here’s an example using surfaces in a sequential model (and I assume objects in an NSC model work the same way):

TheApplication = MATLABZOSConnection(1);
TheSystem = TheApplication.PrimarySystem;
TheLDE = TheSystem.LDE;

N = 3; % number of surfaces
surfs = cell(1,N); % initialize cell array

% loop through the surfaces
for i = 1:N
surfs{i} = TheSystem.LDE.GetSurfaceAt(i);
end

To subsequently access the surfaces/objects, simply refer to them by index number:

 

Specific parameter values for any of the surfaces are also easily accessed:

Regards,

Jeff

Userlevel 2
Badge

Thanks @Jeff.Wilde !

Looks like it works in Matlab as long as you don’t use an array. 

In addition to a cell, I’m guessing a table might work too.

Thanks again,

Josh

Reply