Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Table of Contents

Anchor
MatlabSetup
MatlabSetup
Setting up your Matlab Scripting Environment

These instructions assume that you've already installed OpenSim version 4.0 or later (see Installation Guide for more info). As noted above, OpenSim 4.0 requires 64-bit Matlab.

  1. Launch MATLAB. If you're using Windows and you have OpenSim 3.x configured with Matlab, make sure to "Run as administrator" (available by right-clicking the MATLAB application in the start menu, etc.).
  2. Change your Current Folder to the OpenSim Matlab Scripts directory. On Windows, by default, this is C:/Users/<username>/Documents/OpenSim/4.0/Code/Matlab.
  3. Run the configureOpenSim.m file from the current folder. The script will prompt you to choose your OpenSim installation directory (we will refer to this as OPENSIM_INSTALL_DIR). On Windows, by default, this is C:/OpenSim 4.0
  4. When the script finishes, you should see a dialog box notifying you if the script succeeded. If the script did not succeed, read the output in the command window for more information.
  5. Windows users: ensure that the OPENSIM_INSTALL_DIR/bin directory (e.g., C:/OpenSim 4.0/bin) appears in your system PATH environment variable, and that it appears before any other OpenSim installations you may have on your PATH. The steps to set your path are shown in the animated image below. Double click the image to zoom in. Or see here for additional help on setting your PATH environment variable. Example shown here:
  6. Restart MATLAB; the OpenSim libraries will not be recognized until doing so.
  7. Test that everything is configured correctly: In the command window, run the following:
Code Block
titleTest that OpenSim is properly configured with MATLAB
>>> org.opensim.modeling.opensimCommon.GetVersion()	% This should print the version of OpenSim that you've configured with MATLAB

The configureOpenSim.m file will detect any installations of OpenSim that were previously configured with MATLAB, and will "remove" them from MATLAB (the other OpenSim installations are not deleted, they are simply no longer configured with MATLAB). The configureOpenSim.m file also backs up any changes it makes to MATLAB configuration files.


Expand
titleHaving trouble? Try manual setup...

These manual setup are tailored for Windows and MATLAB 2012b and later, with side notes for macOS and MATLAB 2012a and earlier.

Find the location where you installed OpenSim (e.g., C:\OpenSim 4.0). We will refer to this directory as OPENSIM_INSTALL_DIR. Substitute your specific directory in the instructions below. In the images, we use C:\OpenSim 4.0.

Launch MATLAB

Section


Column


Column
  • If you have MATLAB 2012b or later, you can launch MATLAB regularly.
  • If you have MATLAB 2012a or earlier, you must run MATLAB as an administrator: right-click MATLAB in your Start menu, and select Run as administrator.



Tell MATLAB about OpenSim's Java library (.jar)

Section


Column


Column
  • In the MATLAB Command Window, type edit(fullfile(prefdir, 'javaclasspath.txt')).
    • For MATLAB 2012a or earlier, type edit classpath.txt instead.
  • Add OPENSIM_INSTALL_DIR\sdk\Java\org-opensim-modeling.jar to the end of the file.
  • On macOS, use forward slashes (/) instead of back slashes (\).
  • Remove any entries for previous versions of OpenSim (in either fullfile(prefdir, 'javaclasspath.txt')or classpath.txt, even if you have MATLAB 2012b or later).


Tell Matlab about OpenSim's C++ libraries, which the Java library depends on

Section


Column


Column
  • In the MATLAB Command Window, type edit(fullfile(prefdir, 'javalibrarypath.txt')).
    • For MATLAB 2012a or earlier, type edit librarypath.txt instead.
  • Add OPENSIM_INSTALL_DIR\bin\ to the end of the file.
  • On macOS, use forward slashes (/) instead of back slashes (\).
  • Remove any entries for previous versions of OpenSim (in either fullfile(prefdir, 'javalibrarypath.txt')or librarypath.txt, even if you have MATLAB 2012b or later).


Add the OpenSim Utilities directory to MATLAB's PATH variable

Section


Column


Column
  • In the MATLAB Command Window, type pathtool (or editpath in older versions of MATLAB).
  • Click Add Folder... and select C:\Users\<username>\Documents\OpenSim\4.0\Code\Matlab\Utilities folder (by default).
    • Remove any entries for previous versions of OpenSim.
  • On macOS, you will see forward slashes (/) instead of back slashes (\).


Please report any issues you have with the configureOpenSim.m file so that other users need not perform these manual steps in the future.

Loading OpenSim Libraries 

Once you have set up the OpenSim-Matlab environment you must import the OpenSim libraries to be able to have access to the OpenSim classes. Since imported libraries are not Global variables, you must import the libraries at the beginning of all scripts or functions that use OpenSim classes. This included nested functions or classes. 


Code Block
titleTo Load the OpenSim Libraries in Matlab
>> import org.opensim.modeling.*		% Import OpenSim Libraries


Finding the Methods Available for a Class in the OpenSim Libraries

Often, it is difficult to know exactly what methods are available for an OpenSim class (e.g., Model, Muscle) and so using the API Documentation regularly will be useful. However, you can very quickly get a list of the available methods and the interfaces to those methods by using the built-in Matlab functions methods and methodsview().

methods() will output a list of all the methods available for a given OpenSim class to the MATLAB command window. Below is an example of methods you can call on the Model class. A list of methods is printed to the command window (cut off here since the list is very long).

Code Block
titleView all methods in an OpenSim class
>> methods(Model)
Methods for class Model:

LoadOpenSimLibrary                            getDefaultSubsystem                           notifyAll                                     
Model                                         getDescription                                overrideAllActuators                          
PrintPropertyInfo                             getDiscreteVariableValue                      postScale                                     
RegisterType                                  getDisplayHints                               preScale                                      
RenameType                                    getDocumentFileName                           print                                         
SafeCopy                                      getDocumentFileVersion                        printBasicInfo                                
addAnalysis                                   getFilePath                                   printComponentsMatching                       
addBody                                       getForceSet                                   printControlStorage                           
addComponent                                  getForceSubsystem                             printDetailedInfo                             
addConstraint                                 getForceUnits                                 printInputInfo                                
addContactGeometry                            getFrameList                                  printOutputInfo                               
addController                                 getFunctionClassNames                         printSocketInfo                               
addForce                                      getGravity                                    printSubcomponentInfo                         
addJoint                                      getGravityForce                               readObjectFromXMLNodeOrFile 
...											  ...											...


methodsview() is useful to get a list of the methods and the method signatures (arguments, return type) for an OpenSim class. When calling methodsview(), a Matlab pop-up window will be generated that shows the method names, the arguments you can input, and the return type.

Code Block
titleView all method signatures in an OpenSim class
>> myModel = Model()
>> methodsview(myModel)


Tab-completion can be used to get quick access to a method name and is most useful when you know the method name (or the first few letters of the method name).  In the GIF below, the tab key is pressed after "myModel." is typed to open the tab-completion window.

...

Utilities

OpenSim provides utility functions for some common tasks. If you have installed OpenSim, these scripts are located in the resources directory (e.g., C:/Users/<username>/Documents/OpenSim/4.0/Code/Matlab/Utilities). 

File Name(s)

Description
osimC3D.mImport data from a C3D file to OpenSim Tables. See the c3dExport.m example below for a use case.

osimTableFromStruct.m
osimTableToStruct.m

Convert between an OpenSim Table and Matlab Struct.

These utilities convert between OpenSim TimeSeriesTables (a new storage format from OpenSIm 4.0 for time series data, details below) and Matlab Structs. Be aware that any metadata in your Table will be lost when you convert to a Matlab Struct.

osimVec3FromArray.m
osimVec3ToArray.m
Convert between an OpenSim Vec3 and a Matlab Array.


osimList2MatlabCell.mGet a Matlab cell array populated with an OpenSim's Model's list (e.g., Body). See the section "Using a model's "Lists" through iterators" below for more details and usage.

Example Scripts

...

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.

Show OpenSim's log messages in the Matlab Command Window

Info

Applies to OpenSim 4.2 (unreleased) 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
Logger.addSink(JavaLogSink())


Additional information

...