Page 1 of 1

Opening .sto files in MATLAB

Posted: Mon Jul 17, 2017 2:25 am
by mishapatel
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.

Re: Opening .sto files in MATLAB

Posted: Mon Jul 17, 2017 3:08 am
by mitkof6
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.

Re: Opening .sto files in MATLAB

Posted: Mon Jul 17, 2017 4:49 am
by mishapatel
Thanks
Can I run that code in MATLAB?

Re: Opening .sto files in MATLAB

Posted: Mon Jul 17, 2017 5:42 am
by mitkof6
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!

Re: Opening .sto files in MATLAB

Posted: Mon Jul 17, 2017 6:54 am
by mishapatel
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?

Re: Opening .sto files in MATLAB

Posted: Tue Jul 18, 2017 4:41 am
by student
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

Re: Opening .sto files in MATLAB

Posted: Tue Jul 18, 2017 4:53 am
by tkuchida
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.

Re: Opening .sto files in MATLAB

Posted: Thu Jul 20, 2017 2:35 am
by mishapatel
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?

Re: Opening .sto files in MATLAB

Posted: Thu Jul 20, 2017 3:28 am
by tkuchida
do the labels 'state_01', 'state_02' and 'state_03' refer to x,y,z coordinates of the velocity, acceleration and position?
Yes.