Skip to content

Instantly share code, notes, and snippets.

@mitkof6
Created November 18, 2017 21:15
Show Gist options
  • Save mitkof6/03c887ccc867e1c8976694459a34edc3 to your computer and use it in GitHub Desktop.
Save mitkof6/03c887ccc867e1c8976694459a34edc3 to your computer and use it in GitHub Desktop.
A python function for reading OpenSim .sto files.
import os
def readMotionFile(filename):
""" Reads OpenSim .sto files.
Parameters
----------
filename: absolute path to the .sto file
Returns
-------
header: the header of the .sto
labels: the labels of the columns
data: an array of the data
"""
if not os.path.exists(filename):
print('file do not exists')
file_id = open(filename, 'r')
# read header
next_line = file_id.readline()
header = [next_line]
nc = 0
nr = 0
while not 'endheader' in next_line:
if 'datacolumns' in next_line:
nc = int(next_line[next_line.index(' ') + 1:len(next_line)])
elif 'datarows' in next_line:
nr = int(next_line[next_line.index(' ') + 1:len(next_line)])
elif 'nColumns' in next_line:
nc = int(next_line[next_line.index('=') + 1:len(next_line)])
elif 'nRows' in next_line:
nr = int(next_line[next_line.index('=') + 1:len(next_line)])
next_line = file_id.readline()
header.append(next_line)
# process column labels
next_line = file_id.readline()
if next_line.isspace() == True:
next_line = file_id.readline()
labels = next_line.split()
# get data
data = []
for i in range(1, nr + 1):
d = [float(x) for x in file_id.readline().split()]
data.append(d)
file_id.close()
return header, labels, data
@Mainak1792
Copy link

Thanks. How can I extract a single column? from a .sto file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment