Home > vbmeg > functions > tool_box > dmri_processor > functions > util > read_mrtrix_tracks.m

read_mrtrix_tracks

PURPOSE ^

function: tracks = read_mrtrix_tracks (filename)

SYNOPSIS ^

function tracks = read_mrtrix_tracks (filename)

DESCRIPTION ^

 function: tracks = read_mrtrix_tracks (filename)

 returns a structure containing the header information and data for the MRtrix 
 format track file 'filename' (i.e. files with the extension '.tck'). 
 The track data will be stored as a cell array in the 'data' field of the
 return variable.

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function tracks = read_mrtrix_tracks (filename)
0002 % function: tracks = read_mrtrix_tracks (filename)
0003 %
0004 % returns a structure containing the header information and data for the MRtrix
0005 % format track file 'filename' (i.e. files with the extension '.tck').
0006 % The track data will be stored as a cell array in the 'data' field of the
0007 % return variable.
0008 %
0009 % Copyright (C) 2011, ATR All Rights Reserved.
0010 % License : New BSD License(see VBMEG_LICENSE.txt)
0011 
0012 image.comments = {};
0013 
0014 f = fopen (filename, 'r');
0015 if (f<1) 
0016   disp (['error opening ' filename ]);
0017   return
0018 end
0019 L = fgetl(f);
0020 if ~strncmp(L, 'mrtrix tracks', 13)
0021   fclose(f);
0022   disp ([filename ' is not in MRtrix format']);
0023   return
0024 end
0025 
0026 tracks = struct();
0027 
0028 while 1
0029   L = fgetl(f);
0030   if ~ischar(L), break, end;
0031   L = strtrim(L);
0032   if strcmp(L, 'END'), break, end;
0033   d = strfind (L,':');
0034   if isempty(d)
0035     disp (['invalid line in header: ''' L ''' - ignored']);
0036   else
0037     key = lower(strtrim(L(1:d(1)-1)));
0038     value = strtrim(L(d(1)+1:end));
0039     if strcmp(key, 'file')
0040       file = value;
0041     elseif strcmp(key, 'datatype')
0042       tracks.datatype = value;
0043     else 
0044       tracks = setfield (tracks, key, value);
0045     end
0046   end
0047 end
0048 fclose(f);
0049 
0050 if ~exist ('file') || ~isfield (tracks, 'datatype')
0051   disp ('critical entries missing in header - aborting')
0052   return
0053 end
0054 
0055 [ file, offset ] = strtok(file);
0056 if ~strcmp(file,'.')
0057   disp ('unexpected file entry (should be set to current ''.'') - aborting')
0058   return;
0059 end
0060 
0061 if isempty(offset)
0062   disp ('no offset specified - aborting')
0063   return;
0064 end
0065 offset = str2num(char(offset));
0066 
0067 datatype = lower(tracks.datatype);
0068 byteorder = datatype(end-1:end);
0069 
0070 if strcmp(byteorder, 'le')
0071   f = fopen (filename, 'r', 'l');
0072   datatype = datatype(1:end-2);
0073 elseif strcmp(byteorder, 'be')
0074   f = fopen (filename, 'r', 'b');
0075   datatype = datatype(1:end-2);
0076 else
0077   disp ('unexpected data type - aborting')
0078   return;
0079 end
0080 
0081 if (f<1) 
0082   disp (['error opening ' filename ]);
0083   return
0084 end
0085 
0086 fseek (f, offset, -1);
0087 data = fread(f, inf, datatype);
0088 fclose (f);
0089 
0090 N = floor(prod(size(data))/3);
0091 if size(data,1)*size(data,2) == 3*N % 2012/06/26 M.Fukushima
0092   data = reshape (data, 3, N)';
0093   k = find (~isfinite(data(:,1)));
0094 
0095   tracks.data = {};
0096   pk = 1;
0097   for n = 1:(prod(size(k))-1)
0098     tracks.data{end+1} = data(pk:(k(n)-1),:);
0099     pk = k(n)+1;
0100   end
0101 else
0102   tracks.data = {}; % 2012/06/26 M.Fukushima
0103 end
0104

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005