Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: keep reference to Log in code sample to avoid early gc as reported on forum

...

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
languagejava
titleUse PropertyHelper for classes defined in plugins
collapsetrue
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
titleAdvanced background information on how MATLAB scripting works...

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.

  1. OpenSim's Java library is located in the file org-opensim-modeling.jar in the OpenSim installation. To allow MATLAB to use OpenSim's Java library, the configureOpenSim.m file tells MATLAB where this .jar library is located by adding the path to the .jar file to the javaclasspath.txt file in MATLAB's preferences directory (determined by running prefdirin MATLAB).
  2. The .jar file depends on OpenSim's C++ library osimJavaJNI.dll (or libosimJavaJNI.dylib on macOS), and the configureOpenSim.m file tells MATLAB the location of this library by adding the appropriate directory to the javalibrarypath.txt file in MATLAB's preferences directory.
  3. The osimJavaJNI library depends on OpenSim's other C++ libraries. On Windows, for MATLAB to find these other libraries, the directory containing these libraries must be on the system PATH. This step is not necessary on macOS.
  4. The MATLAB interface also involves a few pure MATLAB functions in the C:/Users/<username>/Documents/OpenSim/4.0/Code/Matlab/Utilities folder (by default). The configureOpenSim.m file adds this directory to the MATLAB path.

...