Opening .sto files in MATLAB

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Misha Patel
Posts: 17
Joined: Mon Jun 05, 2017 2:07 am

Opening .sto files in MATLAB

Post by Misha Patel » Mon Jul 17, 2017 2:25 am

Hi,
I am trying to do a point kinematics analysis to obtain the vectors of velocity of a knee model while it walks.

I have run the analysis and want to see the '.sto' file... In the 'getting started with analyses section of the OpenSim documentation- it says that it is possible to open the file in MATLAB? Can I have some guidance on this please.
Also, is this the correct way to go about getting vectors of velocity?

Thank you.

User avatar
Dimitar Stanev
Posts: 1096
Joined: Fri Jan 31, 2014 5:14 am

Re: Opening .sto files in MATLAB

Post by Dimitar Stanev » Mon Jul 17, 2017 3:08 am

One way is to implement a .sto importer:

Code: Select all

function q = readMotionFile(fname)
% Purpose:  This function reads a file in the format of a SIMM motion file
%           and returns a data structure
%
% Input:    fname is the name of the ascii datafile to be read 
%           ('character array') 
%
% Output:   q returns a structure with the following format:
%				q.labels 	= array of column labels
%				q.data 		= matrix of data
%				q.nr 		= number of matrix rows
%				q.nc 		= number of matrix columns
%
% ASA 12/03
% Modified by Eran Guendelman 09/06
% Open ascii data file for reading.
fid = fopen(fname, 'r');	
if fid == -1								
	error(['unable to open ', fname])		
end
% Process the file header;
% store # data rows, # data columns.
q.nr = 0; % Added to ensure that the q structures from reading a motion file
q.nc = 0; % are always the same, even if nr and nc are different orders in file.
nextline = fgetl(fid);	
while ~strncmpi(nextline, 'endheader', length('endheader'))
	if strncmpi(nextline, 'datacolumns', length('datacolumns'))
		q.nc = str2num(nextline(findstr(nextline, ' ')+1 : length(nextline)));
	elseif strncmpi(nextline, 'datarows', length('datarows'))
		q.nr = str2num(nextline(findstr(nextline, ' ')+1 : length(nextline)));
	elseif strncmpi(nextline, 'nColumns', length('nColumns'))
		q.nc = str2num(nextline(findstr(nextline, '=')+1 : length(nextline)));
	elseif strncmpi(nextline, 'nRows', length('nRows'))
		q.nr = str2num(nextline(findstr(nextline, '=')+1 : length(nextline)));
	end
	nextline = fgetl(fid);
end
% Process the column labels.
nextline = fgetl(fid);
if (all(isspace(nextline))) % Blank line, so the next one must be the one containing the column labels
	nextline = fgetl(fid);
end
q.labels = cell(1, q.nc);
for j = 1:q.nc
	[q.labels{j}, nextline] = strtok(nextline);
end
% Process the data.
% Note:  transpose is needed since fscanf fills columns before rows.
q.data = fscanf(fid, '%f', [q.nc, q.nr])';
fclose(fid);
return;
Yes PointKinematics and BodyKinematics are the correct way to get positions, velocities and accelerations of points and bodies.

User avatar
Misha Patel
Posts: 17
Joined: Mon Jun 05, 2017 2:07 am

Re: Opening .sto files in MATLAB

Post by Misha Patel » Mon Jul 17, 2017 4:49 am

Thanks
Can I run that code in MATLAB?
Last edited by Misha Patel on Thu Jul 20, 2017 12:49 am, edited 3 times in total.

User avatar
Dimitar Stanev
Posts: 1096
Joined: Fri Jan 31, 2014 5:14 am

Re: Opening .sto files in MATLAB

Post by Dimitar Stanev » Mon Jul 17, 2017 5:42 am

This is a Matlab function. Just copy in a separate file with same name (readMotionFile.m) and call the function by providing the absolute path to your .sto file (e.g. q = readMotionFile("my.sto")). Do not change the arguments to fopen!

User avatar
Misha Patel
Posts: 17
Joined: Mon Jun 05, 2017 2:07 am

Re: Opening .sto files in MATLAB

Post by Misha Patel » Mon Jul 17, 2017 6:54 am

oh thanks. I have it now. As expected, 'q' appears in my workspace. What do the different 'states' mean in the data section? currently only the time is giving me data. the rest of the columns give me 0's... is this to do with how I've carried out the point kinematics trial?

User avatar
shayan moradkhani
Posts: 41
Joined: Fri May 05, 2017 5:12 pm

Re: Opening .sto files in MATLAB

Post by shayan moradkhani » Tue Jul 18, 2017 4:41 am

hi,
i have carried out a pointkinematics and a part of my results are attached. my concern is time steps. is there any way to get times with a certain time step in .mot or .sto files. what i mean is, the time is not uniformly stated here. i am trying to have time recordings like from 0, 0.1, 0.2,... .
it would be good a if could find a way to do that.
regards
Shayan
Attachments
Capture.PNG
Capture.PNG (14.84 KiB) Viewed 458 times

User avatar
Thomas Uchida
Posts: 1776
Joined: Wed May 16, 2012 11:40 am

Re: Opening .sto files in MATLAB

Post by Thomas Uchida » Tue Jul 18, 2017 4:53 am

is there any way to get times with a certain time step in .mot or .sto files.
You can resample the data in MATLAB or Python using an appropriate interpolation strategy.

User avatar
Misha Patel
Posts: 17
Joined: Mon Jun 05, 2017 2:07 am

Re: Opening .sto files in MATLAB

Post by Misha Patel » Thu Jul 20, 2017 2:35 am

with respect to the data that has been processed using that MATLAB code, do the labels 'state_01', 'state_02' and 'state_03' refer to x,y,z coordinates of the velocity, acceleration and position?

User avatar
Thomas Uchida
Posts: 1776
Joined: Wed May 16, 2012 11:40 am

Re: Opening .sto files in MATLAB

Post by Thomas Uchida » Thu Jul 20, 2017 3:28 am

do the labels 'state_01', 'state_02' and 'state_03' refer to x,y,z coordinates of the velocity, acceleration and position?
Yes.

POST REPLY