You are viewing the documentation for OpenSim 3.x. Are you looking for the latest OpenSim 4.0 Documentation?
Scripting with Matlab
The system architecture of Matlab & OpenSim must match (install 64-bit OpenSim if you use 64-bit Matlab). Matlab setup and configuration instructions are below.
When referring to indexed elements remember that Matlab begins indexing at 1 while OpenSim data structures begin at 0.
What's Available?
- Access to the OpenSim API to create and simulate models
- Users can instantiate and run tools from setup files or programmatically
- Repeated/batch runs of tools
- A visualizer for API users that can be invoked through Matlab
Limitations
- In this mode, there’s no access to the plotter (you can use Matlab’s native plotter) or the graphics window (use the model visualizer instead).
- Some internal Simtk classes (that belong to the SimTK namespace and simbody internals) are not available for construction, but the most commonly used classes are available as of version 3.1 (SimTK::Vec3, Vector, Mat33, State, Inertia).
Loading OpenSim Libraries
>> import org.opensim.modeling.* % Import OpenSim Libraries
To view the methods in an OpenSim class, you can use the following in the MATLAB command window:
>> methods Model >> methods org.opensim.modeling.Model
You can obtain a window listing the signatures (arguments, return type) of all methods in an OpenSim class:
>> methodsview Model
The MATLAB command window also supports tab completion.
Available Example Scripts
Scripts can are located in the OpenSim distribution in the /Scripts/Matlab folder.
Script Name | Description |
---|---|
and | These scripts generate a simple tug of war model, run a simulation, and visualize the results. |
Script to create an OpenSim model with coordinates prescribed according to an input motion file. It uses a helper function defined in the script createPrescribedMotionModel. | |
This is a Matlab function that generates OpenSim Setup Files for the Analyze>MuscleAnalysis tool and runs the Analyze tool. You must run setupAndRunIKBatchExample first to generate the motion files used in this example. | |
Runs multiple inverse kinematics trials for the model Subject01 located in the subfolder "testData\subject01". User must specify the results directory (e.g. "IKResults"), the .osim model (e.g. "subject01_gait2392_scaled.osim"), and the .trc files (e.g. in folder "MarkerData"). To see the results, load the model and IK output in the GUI. | |
Runs a simple optimization to find the elbow flexion angle where the moment arm of the biceps femoris short head is maximized. | |
Runs model simulation for gvien coefficients, returns integration | |
Function that loads a model and its muscles, then creates a new model where all muscles are scaled by a common scale factor. Takes 3 inputs: 1) the path and name of an existing OpenSim model, 2) the path and name of the new model to be outputted, and 3) the scale factor. If no input model names are given, the user is prompted to choose a model to scale and all muscle strengths are doubled. |
Setting up your Matlab Scripting Environment
These instructions assume that you've already installed OpenSim version 3.1 or later (see Installation Guide for more info). As noted above, the system architecture of OpenSim must match your version of MATLAB (64-bit or 32-bit). Windows users: note that the OPENSIM_INSTALL_DIR\bin directory must appear in your system PATH environment variable.
Automated Setup
Manual Setup
Handling Java Exceptions
By default, if Java exceptions occur, Matlab will exit and save debug information to a log file in the current workspace. If this happens please send us the stack trace or crash report to fix. You don't need to setDebugLevel as of version 3.1. For previous versions you can call
OpenSimObject.setDebugLevel(3);
Troubleshooting
- Errors related to osimJavaJNI not being on the path or "java.lang.NoClassDefFoundError" occur because the OPENSIM_INSTALL_DIR\bin directory is not on the system (Windows) path. Be sure the OPENSIM_INSTALL_DIR\bin directory has been added to the system PATH environment variable. (Note that the system PATH environment variable is distinct from MATLAB's path variable.)
- If you're seeing errors saying failed dependencies with osimJavaJNI.dll, you would need to have Visual Studio redistribution package installed (which would come with any Visual Studio installation). You can just install the package files without Visual Studio on the Microsoft download site (For example 32-bit VS 2010 package is located here).
OpenSim is supported by the Mobilize Center , an NIH Biomedical Technology Resource Center (grant P41 EB027060); the Restore Center , an NIH-funded Medical Rehabilitation Research Resource Network Center (grant P2C HD101913); and the Wu Tsai Human Performance Alliance through the Joe and Clara Tsai Foundation. See the People page for a list of the many people who have contributed to the OpenSim project over the years. ©2010-2024 OpenSim. All rights reserved.