Hello all,
I have a question about OpenSim through Matlab.
How can we display and play the final model with its motion through Matlab to see the animation? I couldn't find the command in Matlab.
Thanks.
Best,
Arash
Opensim and Matlab
- Ana de Sousa
- Posts: 62
- Joined: Thu Apr 07, 2016 4:21 pm
Re: Opensim and Matlab
Hello Arash,
I do not know if Matlab opens OpenSim and starts the animation. What I do is the following:
1) I create an .sto file for visualization
2) I open the OpenSim GUI
3) I open the model and load the .sto
My code for creating the visualization file:
Nsamples = length(motionData.data(:,1)); Nstates = length(statesNames); %motionData is the return from "IntegrateOpenSimPlant" function
str = strjoin(statesNames,'\t'); %variable with the names of the states
header = ['gait_simulation \nversion=1 \nnRows=' num2str(Nsamples) ' \nnColumns=' num2str(Nstates+1) '\ninDegrees=no \nendheader \ntime ' str '\n']; % create the header of the .sto file
fid = fopen('Results/gait_simulation.sto','wt'); %open the file
fprintf(fid,header); fclose(fid); % print the header
fid = fopen('Results/gait_simulation.sto','a+');
for i = 1:Nsamples
fprintf(fid,'\t%f',motionData.data(i,:)); fprintf(fid,'\n'); % print the states
end
fclose(fid); %close the file
That is working fine for me, I hope it helps.
I do not know if Matlab opens OpenSim and starts the animation. What I do is the following:
1) I create an .sto file for visualization
2) I open the OpenSim GUI
3) I open the model and load the .sto
My code for creating the visualization file:
Nsamples = length(motionData.data(:,1)); Nstates = length(statesNames); %motionData is the return from "IntegrateOpenSimPlant" function
str = strjoin(statesNames,'\t'); %variable with the names of the states
header = ['gait_simulation \nversion=1 \nnRows=' num2str(Nsamples) ' \nnColumns=' num2str(Nstates+1) '\ninDegrees=no \nendheader \ntime ' str '\n']; % create the header of the .sto file
fid = fopen('Results/gait_simulation.sto','wt'); %open the file
fprintf(fid,header); fclose(fid); % print the header
fid = fopen('Results/gait_simulation.sto','a+');
for i = 1:Nsamples
fprintf(fid,'\t%f',motionData.data(i,:)); fprintf(fid,'\n'); % print the states
end
fclose(fid); %close the file
That is working fine for me, I hope it helps.
- Christopher Dembia
- Posts: 506
- Joined: Fri Oct 12, 2012 4:09 pm
Re: Opensim and Matlab
You can also use the Simbody Visualizer through Matlab. Use "model.setUseVisualizer(true)" right after constructing your model. If you are using 4.0, you can see the following example, which uses the visualizer for a forward simulation: https://github.com/opensim-org/opensim- ... isualize.m
- Arash Mohammadzadeh Gonabadi
- Posts: 34
- Joined: Sun Sep 10, 2017 10:44 am
Re: Opensim and Matlab
Hello,
Thank you so much for your help. I tested "model.setUseVisualizer(true)" previously, but unfortunately it did not work, and I was wondering what is my mistake.
Would it be possible to take a look at the following file and run it with Matlab? Thank you so much.
best,
Arash
Thank you so much for your help. I tested "model.setUseVisualizer(true)" previously, but unfortunately it did not work, and I was wondering what is my mistake.
Would it be possible to take a look at the following file and run it with Matlab? Thank you so much.
best,
Arash