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!
Importing TRC Into Matlab
- Hafizur Rahman
- Posts: 13
- Joined: Thu Mar 03, 2016 2:24 pm
Re: Importing TRC Into Matlab
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,
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,
- Gavin Lenton
- Posts: 7
- Joined: Mon Feb 17, 2014 5:19 pm
Re: Importing TRC Into Matlab
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.
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.
- Aaron Godfrey
- Posts: 55
- Joined: Tue Feb 16, 2016 12:34 pm
Re: Importing TRC Into Matlab
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
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
- Gavin Lenton
- Posts: 7
- Joined: Mon Feb 17, 2014 5:19 pm
Re: Importing TRC Into Matlab
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.
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.
- Aaron Godfrey
- Posts: 55
- Joined: Tue Feb 16, 2016 12:34 pm
Re: Importing TRC Into Matlab
Unfortunately, I can't share a .trc file. I'll have to figure something else out, I guess... Thanks anyways!
- shayan moradkhani
- Posts: 41
- Joined: Fri May 05, 2017 5:12 pm
Re: Importing TRC Into Matlab
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
- Gavin Lenton
- Posts: 7
- Joined: Mon Feb 17, 2014 5:19 pm
Re: Importing TRC Into Matlab
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.
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.
Regards,
Gavin.
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);
Gavin.