FOR EACH object in object
1.371 views (last 30 days)
I am trying to find a MATLAB equivalent to FOR EACH object IN object such as in vba/c++.
Private Sub cel_OnQueryProgress(ByVal query As (CQG.ICQGOrdersQuery, ByVal Error As CQG.ICQGError)
Dim Query as CQGOrdersQuery
Dim Order As CQGOrder
For Each order In Query
Todd Flanagan on 31 Mar 2011
for in MATLAB behaves a lot like FOR EACH.
If you say:
for idx = 1:10
you are saying
FOR EACH idx IN [1 2 3 4 5 6 7 8 9 10]
So you can do stuff like:
>> group = ['a' 'b' 'c'];
>> for member = group
disp(['the member is ' member])
the member is a
the member is b
the member is c
Geoff Olynyk on 4 Apr 2012
This has stumped me for the past week, but I think I have it cracked. (At least, this makes sense for my application, which is controlling Siemens Solid Edge ST2 via the COM API. Hopefully you can confirm for your application?)
Visual Basic has the shorthand For Each Object In CollectionClass, which takes advantage of a special field (attribute) Count and a method Item() belonging to "collection class" objects, which are designed to collect other objects (cells in a spreadsheet, 2D lines in a CAD drawing, etc.) See the MSDN reference below.
The field Count just returns the number of Objects in the CollectionClass (an integer), and the method Item(i) returns the i'th Object in the CollectionClass. (In your case, the i'th Order in the Query collection class.)
So to replicate in MATLAB, you would do:
objQuery = CQG.ICQGOrdersQuery ; % define collection class object
nOrders = objQuery.Count ; % number of orders in Query collection class
for k = 1:nOrders, ; % loop through objects in collection class
objOrder = objQuery.Item(k) ; % get handle for Order object
Like I said, this works for my problem, trying to loop through things like reference planes, lines, circles, etc. via the COM API for Solid Edge. Let me know if this makes sense for you!
http://stackoverflow.com/questions/10000289/how-do-i-loop-through-every-object-in-a-class-like-vb-for-each-x-in-y-in-matla (this is me asking the same question as you do here, on Stack Overflow)