Page History
...
Using a model's "Lists" through iterators
Lists, such as BodyList and MuscleList, are useful ways to get access to all components of that type in the model. Access is available through an iterator. Iterators are a different way of getting references to objects in the model and Matlab users may be unfamiliar with them. For a discussion of iterators, see this StackOverflow discussion.
Iterate over all bodies in a model (even bodies not in the model's BodySet)
Code Block |
---|
model = Model("my_model.osim") bodyList = model.getBodyList(); # Get the Model's BodyList iter = bodyList.begin(); # Start the iterator at the beginning of the list while ~iter.equals(bodyList.end()) # Stay in the loop until the iterator reaches the end of the list iter.getName() # Print name to console iter.next() # Move the iterator to the next body in the list end |
Getting a single reference from a list
We have included a Matlab function— osimList2MatlabCell() — that converts an OpenSim List to a Matlab cell array. Then, you can use simple Matlab methods to get references to objects.
Code Block |
---|
model = Model("my_model.osim") references = osimList2MatlabCell(model,'Body') % Get a cell array of references to all the bodies in a model Pelvis = references{1} % Get the first body in the list. |
Using PropertyHelper to set Property values for plugin classes
Classes defined in plugins are not available for direct access in Matlab, however classes that are subclasses of OpenSim "Object" contain properties that can be accessed and modified using the PropertyHelper class. The exact syntax depends on the basic data type of the property, for example
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
prop = obj.getPropertyByName("propertyName") currentValue = PropertyHelper.getValueDouble(prop) PropertyHelper.setValueDouble(newValue, prop) |
Show OpenSim's log messages in the Matlab Command Window
Info |
---|
Applies to OpenSim 4.2 and above. |
Some versions of Matlab do not show OpenSim's log messages by default. To see these messages in the Matlab Command Window, run the following command:
Code Block |
---|
myMatlabLog = JavaLogSink() Logger.addSink(JavaLogSink()myMatlabLog) |
Additional information
Expand | ||
---|---|---|
| ||
MATLAB allows one to load and use Java libraries. OpenSim has a Java interface/library (used by the GUI), allowing us to leverage MATLAB's Java capabilities to provide a MATLAB interface to OpenSim.
|
...