.tck to ascii conversion

Hi guys,

I used the tckconvert command to convert my track-files to .txt files. It seems though, as if always only one single track gets written into the text files. How can I convert the whole .tck file (with 10000 tracks) to a ascii file so I can read it into Matlab?

Hi Dario,

You are correct, if you convert .tck files to ascii, each streamline is saved in a separate file. If you would want to save them all to a single ascii file, some kind of delimiter would have to be defined indicating the end of one streamline and the start of the next. The choice of such delimiter could again raise some ambiguity between different software packages, which is why we chose to output separate files.

If your aim is to read the tracks in Matlab, though, I recommend that you take a look at the read_mrtrix_tracks Matlab function that comes provided with your install in the mrtrix3/matlab folder. You can use this function to read all streamlines directly from the .tck file, without the need for any intermediate ascii conversion.



Awesome, Daan! That’s exactly what I was looking for but since all the information I found about the read_mrtrix_tracks.m was in the documentation of MRtrix 0.2, I figured it was somehow replaced by a proprietary MRtrix function.
One question though: When I used tckconvert, it only gave me one single ascii file, not one for each streamline. How could I theoretically get all streamlines?

Off the top of my head, I think you can do tckconvert tracks.tck track-[].txt and the command will generate files track-0001.txt, track-0002.txt, etc. You could then easily script a loop in Matlab or any other programming language that loops over all these ascii files.

Good idea. I’ll try that.
The Matlab script gives me an error, saying that the “Number of columns on line 26 of ASCII file tracks.tck must be the same as previous lines.” What am I doing wrong?

That’s weird. I must say I haven’t used that script in years since I’m not a Matlab user anymore, so I’m a bit in the dark on its internals. It looks like the Matlab function either has trouble reading the file header (which would then also be exceptionally long, seeing how it mentions line 26), or is somehow tricked into thinking the streamlines are encoded in ascii (while they should be binary floats). Are you sure your .tck file is uncorrupted (check in mrview for instance)? If it’s fine, you may want to step through the function line by line with the Matlab debugger to find out at which point the error occurs.

Okay I just wrote my own little script to extract the tracks with Matlab.
Now I was wondering, since I only want to visualise certain tracks, how can I write only some of the tracks (all in their separate ASCII file) into one .tck file?

Great. Did you happen to find the source of your error with read_mrtrix_tracks in the process? It would be good if we could fix that.

If you now select a subset of the streamlines and have those in separate ascii files, you can again use tckconvert to get them back into mrtrix’ .tck format, it works both ways.

Sorry, I didn’t use the read_mrtrix_tracks. I just wrote a script that was able to select the 50 or so longest tracks by reading the length file I got from tckstats.
Okay, tckconvert works for a single ASCII file, but how do I go about if I have a folder with 50 separate ascii files and I want to write all of them into one single .tck file? What’s the syntax for that?

I have a feeling the syntax would use number sequences along with the square bracket notation used for images. So this would look like:

tckconvert tracks-[].txt tracks.tck

to read all of the matching files, or something like:

tckconvert tracks-[0:49].txt tracks.tck

for a specific subset.