Importing TRC Into Matlab

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Aaron Godfrey
Posts: 55
Joined: Tue Feb 16, 2016 12:34 pm

Importing TRC Into Matlab

Post by Aaron Godfrey » Wed Nov 23, 2016 9:22 am

Hi Everyone,
I'm looking to import trc files into Matlab, and part of that is being able to read the marker names in row 4. However, the textdata in these files seems to be delimited differently than the numeric data, and it's causing a lot of problems trying to read in the data and manipulate it. Have any of you done anything similar, or been able to successfully import trc data somehow? I can get numeric data in just fine, but no matter how I try to import it (with different delimiters and different headerIn values), the textdata never works out.

Thanks!

User avatar
Hafizur Rahman
Posts: 13
Joined: Thu Mar 03, 2016 2:24 pm

Re: Importing TRC Into Matlab

Post by Hafizur Rahman » Wed Nov 23, 2016 4:02 pm

Hi Aaron,

Just wondering, are you trying to write the *.trc files in Matlab? For my case, I wrote the Matlab code to extract the motion data from an excel file and wrote the *.trc file in Matlab in excel format, and then save as *.trc files. For importing both text data and numeric data in Matlab, you need to follow the following formula:

[a, b] = xlsread()

where, a = number data, and b = text data.

You can see the following link, how to import both data types together in Matlab.
https://www.mathworks.com/help/matlab/ref/xlsread.html

Thanks,

User avatar
Gavin Lenton
Posts: 7
Joined: Mon Feb 17, 2014 5:19 pm

Re: Importing TRC Into Matlab

Post by Gavin Lenton » Wed Nov 23, 2016 5:43 pm

Hi Aaron,

Code exists already to read .trc, .mot, and .sto OpenSim output files in Matlab. See https://simtk.org/frs/?group_id=660 for helpful functions you can download. The function 'load_sto_file' in that package also works for .trc and .mot files.

Alternatively, you can use file=importdata(fullfilename) which outputs the numeric data and column headers.

All the best,

Gavin.

User avatar
Aaron Godfrey
Posts: 55
Joined: Tue Feb 16, 2016 12:34 pm

Re: Importing TRC Into Matlab

Post by Aaron Godfrey » Mon Nov 28, 2016 5:49 am

Hi All,
Thanks for the responses!
Hafizur - I am looking to import .trc data, rather than export. I've tried xlsread() before, but it always returns an error since .trc isn't an Excel format.

Gavin - I just downloaded that package. Thanks for the link! Trying to run load_sto_file doesn't seem to be too fruitful, though. It doesn't seem able to read the data_label from the trc file. The specific error:

Undefined function or variable 'data_label'.

Error in load_sto_file (line 46)
for i = 1:length(data_label)

The first few rows & columns of the .trc look like this (copy/pasted so pardon crap format):

PathFileType 4 (X/Y/Z) (filename)
DataRate CameraRate NumFrames NumMarkers Units OrigDataRate OrigDataStartFrame OrigNumFrames
100 100 1634 54 mm 100 1 2046
Frame# Time HEAD RSHO
X1 Y1 Z1 X2 Y2 Z2

291 2.91 515.792489 1239.442587 508.867383 666.594923 952.525198 463.080198
292 2.92 515.585482 1245.711923 509.544075 666.096389 958.743691 463.201314
293 2.93 515.077412 1252.266049 509.703279 665.627122 965.501189 463.230044

Since it's hard to see here, row 4 consists of the Frame # & Time headers followed by each marker every third column from there, with row 5 labeling X/Y/Z. For example, in excel, 'HEAD' is C4, D4 & E4 are blank, and 'X1' is C5, with 'Y1' and "Z1' being D5 & E5 respectively. Subsequent markers are labeled sequentially such that RSHO is X2/Y2/Z2 and the next marker is X3/Y3/Z3.

Does any of this shed light on the problem? I can't help but wonder if it's the fact that there's two actual sets of data here: the mini table of setup info in rows 2&3 and then the actual data in row 4 and beyond. Anyways, I'll keep fiddling with it, and see if I can get anywhere with it, but any further guidance is much appreciated.

Thanks!
-Aaron

User avatar
Gavin Lenton
Posts: 7
Joined: Mon Feb 17, 2014 5:19 pm

Re: Importing TRC Into Matlab

Post by Gavin Lenton » Wed Nov 30, 2016 12:13 am

Hi Aaron,

Ok, it seems as though there is an issue with the 'readtext' function. If you are still experiencing issues can you please attach one of your .trc files and I can try to load it?

Cheers,

Gavin.

User avatar
Aaron Godfrey
Posts: 55
Joined: Tue Feb 16, 2016 12:34 pm

Re: Importing TRC Into Matlab

Post by Aaron Godfrey » Thu Dec 01, 2016 5:17 am

Unfortunately, I can't share a .trc file. I'll have to figure something else out, I guess... Thanks anyways!

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

Re: Importing TRC Into Matlab

Post by shayan moradkhani » Fri Jul 07, 2017 5:04 am

gavinl23 wrote:Hi Aaron,

Code exists already to read .trc, .mot, and .sto OpenSim output files in Matlab. See https://simtk.org/frs/?group_id=660 for helpful functions you can download. The function 'load_sto_file' in that package also works for .trc and .mot files.

Alternatively, you can use file=importdata(fullfilename) which outputs the numeric data and column headers.

All the best,

Gavin.

hi,
would you be kind enough telling how to extract a certain column from .mot or .sto files in Matlab? i downloaded the package and had a look through the load_sto_file.m, however, i could not figure out how to extract a particular column? (copy pasting to excel and reading from there would be good for small number of data, but i have quite huge amounts)
thank you in advance for your help
Shayan

User avatar
Gavin Lenton
Posts: 7
Joined: Mon Feb 17, 2014 5:19 pm

Re: Importing TRC Into Matlab

Post by Gavin Lenton » Sun Jul 09, 2017 11:42 pm

Hi Shayan,

You can extract a certain column by first importing the data and, second, by choosing which column headings to extract the data for.

Code: Select all

filename = 'example.trc';
file=importdata(filename);
yValues=file.colheaders(2:end); %takes all column names except time 

Instead of taking all columns except time, you can index to the specific column you are interested in (you need to know the column name and/or its corresponding index). Then you can specify which column of data to take.

Code: Select all

columnIndex = 5; %column of data you want to extract
results = file.data(:, columnIndex);
Regards,

Gavin.

POST REPLY