MATLAB Answers

1

FOR EACH object in object

Julian 님이 질문을 제출함. 31 Mar 2011
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
Next

  댓글 수: 0

로그인 to comment.

태그

제품

답변 수: 3

Answer by 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])
end
the member is a
the member is b
the member is c
>>

  댓글 수: 4

표시 이전 댓글 수: 1
Walter Roberson
on 31 Mar 2011
I am not sure what you mean by saying that the object "contains" child objects? Do you mean you have a class and wish to find all living objects that are members of the class, even ones in different workspaces ?
Todd Flanagan on 31 Mar 2011
I think what you are seeing is a bit of convenience from VB. CQGOrdersQuery has a property named Orders that returns a collection of CQGOrder. When you use FOR EACH on the Query Object, I'm guessing that it is getting the Collection from the Orders property by convention. MATLAB isn't quite set up that way.
Geoff Olynyk on 30 Mar 2012
I'm running into this same problem trying to use the Solid Edge COM API from Matlab (this is a drafting program). There are many objects, say, 2D lines in my drawing, which I'm trying to get the properties of from Matlab. In VB.NET, you would call it something like:
For Each line2d As SolidEdgeFrameworkSupport.Line2d In dftDoc.ActiveSheet.Lines2d
Console.WriteLine(line2d.Name)
Next
What's the equivalent in MATLAB? How do I get the Name (or any other field) of every Lines2D object in the document?

로그인 to comment.


Answer by Geoff Olynyk on 4 Apr 2012

Julian,
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
objOrder.DoStuff() ;
end
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!
References:

  댓글 수: 0

로그인 to comment.


Answer by Walter Roberson
on 31 Mar 2011

I am not familiar with VBA, so I might well be missing something, but in MATLAB you can do
for order = Query
...
end

  댓글 수: 0

로그인 to comment.



Translated by