Home > SurfStat > SurfStatReadData.m

SurfStatReadData

PURPOSE ^

Reads data (e.g. thickness) from an array of .txt or FreeSurfer files.

SYNOPSIS ^

function data = SurfStatReadData( filenames, dirname, maxmem )

DESCRIPTION ^

Reads data (e.g. thickness) from an array of .txt or FreeSurfer files. 

 Usage: data = SurfStatReadData( filenames [, dirname [, maxmem ]] );

 filenames = .txt, .mgh or FS file name (n=1) or n x k cell array of file 
             names. If the .mgh file has data from n surfaces, then
             filenames must be a file name or 1 x k cell array of file 
             names.
 dirname   = name of a directory where you have write permission. This
             is only needed if the data is memory mapped. The data is
             memory mapped only if it exceeds maxmem Mb as 4 byte reals.
             SurfStatReadData then writes the data as 4 byte reals to a
             temporary file in dirname, then memory maps this file to the
             output data. If dirname does not exist, SurfStatReadVol will
             create it. The default is
           = (filenames{1,1} directory)/SurfStat, so you can ignore this
             parameter if you have write permission in the filenames{1,1} 
             directory.
 maxmem    = memory limit in Mb. The default is the gloabl MAXMEM, or 64. 

 data = n x v matrix of data, v=#vertices, or memory map of same. Data
        from the k files are concatenated. Note that the mapped file is
        not deleted after you quit MATLAB.
 If n=1, data is double precision; 
 if n>1, data is single precision.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function data = SurfStatReadData( filenames, dirname, maxmem )
0002 
0003 %Reads data (e.g. thickness) from an array of .txt or FreeSurfer files.
0004 %
0005 % Usage: data = SurfStatReadData( filenames [, dirname [, maxmem ]] );
0006 %
0007 % filenames = .txt, .mgh or FS file name (n=1) or n x k cell array of file
0008 %             names. If the .mgh file has data from n surfaces, then
0009 %             filenames must be a file name or 1 x k cell array of file
0010 %             names.
0011 % dirname   = name of a directory where you have write permission. This
0012 %             is only needed if the data is memory mapped. The data is
0013 %             memory mapped only if it exceeds maxmem Mb as 4 byte reals.
0014 %             SurfStatReadData then writes the data as 4 byte reals to a
0015 %             temporary file in dirname, then memory maps this file to the
0016 %             output data. If dirname does not exist, SurfStatReadVol will
0017 %             create it. The default is
0018 %           = (filenames{1,1} directory)/SurfStat, so you can ignore this
0019 %             parameter if you have write permission in the filenames{1,1}
0020 %             directory.
0021 % maxmem    = memory limit in Mb. The default is the gloabl MAXMEM, or 64.
0022 %
0023 % data = n x v matrix of data, v=#vertices, or memory map of same. Data
0024 %        from the k files are concatenated. Note that the mapped file is
0025 %        not deleted after you quit MATLAB.
0026 % If n=1, data is double precision;
0027 % if n>1, data is single precision.
0028 
0029 global MAXMEM
0030 if nargin<3
0031     if isempty(MAXMEM)
0032         maxmem=64;
0033     else
0034         maxmem=MAXMEM;
0035     end
0036 end
0037 maxmem=maxmem*2^20;
0038 
0039 if isstr(filenames)
0040     sf=filenames;
0041     filenames=cell(1,1);
0042     filenames(1)={sf};
0043 end
0044 
0045 [n,k]=size(filenames);
0046 if n>1
0047     fprintf(1,'%s',[num2str(n) ' x ' num2str(k) ' files to read, % remaining: 100 ']);
0048 end
0049 data=[];
0050 vs=zeros(1,k);
0051 for j=1:k
0052     data1=SurfStatReadData1(filenames{1,j});
0053     if size(data1,1)==1
0054         data=[data data1];
0055     else
0056         data=[data single(data1)];
0057     end        
0058     vs(j)=size(data1,2);
0059 end
0060 
0061 if n>1
0062     n10=floor(n/10);
0063     data1=data;
0064     v=size(data1,2);
0065     isnum=(n*v*4<=maxmem);
0066     if isnum
0067         data=zeros(n,v,'single');
0068         data(1,:)=single(data1);
0069     else
0070         if nargin<2 | isempty(dirname)
0071             [PATHSTR,NAME,EXT]=fileparts(filenames{1,1});
0072             dirname=fullfile(PATHSTR,'SurfStat');
0073         end
0074         if ~exist(dirname,'dir')
0075             [SUCCESS,MESSAGE,MESSAGEID]=mkdir(dirname);
0076             if ~SUCCESS
0077                 error(MESSAGEID,['Tried to make directory ' dirname ' for memory mapping. \n',...
0078                     'Please specify the name of a directory where you have write permission \n',...
0079                     'as the second parameter of SurfStatReadData - see the help :-) Error: \n',...
0080                     MESSAGE]);
0081             end
0082         end
0083         [PATHSTR,NAME,EXT]=fileparts(tempname);
0084         Filename=fullfile(dirname,NAME);
0085         fid=fopen(Filename,'wb');
0086         fwrite(fid,data1,'single');
0087     end
0088     v2=cumsum(vs);
0089     v1=v2-vs+1;
0090     for i=2:n
0091         if rem(i,n10)==0 
0092             fprintf(1,'%s',[num2str(round(100*(1-i/n))) ' ']);
0093         end
0094         for j=1:k
0095             data1=SurfStatReadData1(filenames{i,j});
0096             if isnum
0097                 data(i,v1(j):v2(j))=single(data1);
0098             else
0099                 fwrite(fid,data1,'single');
0100             end
0101         end
0102     end
0103     fprintf(1,'%s\n','Done');
0104     if ~isnum
0105         fclose(fid);
0106         data=memmapfile(Filename,'Format',{'single' [v n] 'Data'},'Writable',true)
0107     end
0108 end
0109     
0110 return
0111 end
0112 
0113

Generated on Fri 26-Sep-2008 14:05:29 by m2html © 2003