Reads coordinates and triangles from an array of .obj or FreeSurfer files. Usage: [ surf, ab ] = SurfStatReadSurf( filenames [,ab [,numfields ... [, dirname [, maxmem ] ] ] ] ); filenames = .obj or FS file name (n=1) or n x k cell array of file names. ab = 'a' for ASCII or 'b' for binary. If it doesn't work it will try the other. Default is 'a'. numfields = number of fields to read, in the order below, default 2. 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. SurfStatReadSurf 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 64. surf.tri = t x 3 matrix of triangle indices, 1-based, t=#triangles. surf.coord = 3 x v matrix of coordinates, v=#vertices, if n=1, or n x v x 3 array if n>1, or memory map of same. Data from the k files are concatenated. Note that the mapped file is not deleted after you quit MATLAB. ab = whichever was successful. The coordinates and triangle indices of the k files are concatenated. If n=1, surf.coord is double precision; if n>1, surf.coord is single precision. surf.tri is int32.
0001 function [surf,ab]=SurfStatReadSurf(filenames,ab,numfields,dirname,maxmem); 0002 0003 %Reads coordinates and triangles from an array of .obj or FreeSurfer files. 0004 % 0005 % Usage: [ surf, ab ] = SurfStatReadSurf( filenames [,ab [,numfields ... 0006 % [, dirname [, maxmem ] ] ] ] ); 0007 % 0008 % filenames = .obj or FS file name (n=1) or n x k cell array of file names. 0009 % ab = 'a' for ASCII or 'b' for binary. If it doesn't work it 0010 % will try the other. Default is 'a'. 0011 % numfields = number of fields to read, in the order below, default 2. 0012 % dirname = name of a directory where you have write permission. This 0013 % is only needed if the data is memory mapped. The data is 0014 % memory mapped only if it exceeds maxmem Mb as 4 byte reals. 0015 % SurfStatReadSurf then writes the data as 4 byte reals to a 0016 % temporary file in dirname, then memory maps this file to the 0017 % output data. If dirname does not exist, SurfStatReadVol will 0018 % create it. The default is 0019 % = (filenames{1,1} directory)/SurfStat, so you can ignore this 0020 % parameter if you have write permission in the filenames{1,1} 0021 % directory. 0022 % maxmem = memory limit in Mb. The default is 64. 0023 % 0024 % surf.tri = t x 3 matrix of triangle indices, 1-based, t=#triangles. 0025 % surf.coord = 3 x v matrix of coordinates, v=#vertices, if n=1, or 0026 % n x v x 3 array if n>1, or memory map of same. Data from the 0027 % k files are concatenated. Note that the mapped file is not 0028 % deleted after you quit MATLAB. 0029 % ab = whichever was successful. 0030 % The coordinates and triangle indices of the k files are concatenated. 0031 % If n=1, surf.coord is double precision; 0032 % if n>1, surf.coord is single precision. 0033 % surf.tri is int32. 0034 0035 global MAXMEM 0036 if nargin<5 0037 if isempty(MAXMEM) 0038 maxmem=64; 0039 else 0040 maxmem=MAXMEM; 0041 end 0042 end 0043 maxmem=maxmem*2^20; 0044 0045 if nargin<2 | isempty(ab) 0046 ab='a'; 0047 end 0048 if nargin<3 | isempty(numfields) 0049 numfields=2; 0050 end 0051 numfields1=(numfields-1)*3+1; 0052 0053 if isstr(filenames) 0054 sf=filenames; 0055 filenames=cell(1,1); 0056 filenames(1)={sf}; 0057 end 0058 0059 [n,k]=size(filenames); 0060 if n>1 0061 fprintf(1,'%s',[num2str(n) ' x ' num2str(k) ' files to read, % remaining: 100 ']); 0062 end 0063 c=[]; 0064 vs=zeros(1,k); 0065 if numfields==2 0066 surf.tri=[]; 0067 end 0068 for j=1:k 0069 [s,ab]=SurfStatReadSurf1(filenames{1,j},ab,numfields1); 0070 if numfields==2 0071 surf.tri=[surf.tri; int32(s.tri)+size(c,2)]; 0072 end 0073 c=[c s.coord]; 0074 vs(j)=size(s.coord,2); 0075 end 0076 0077 if n==1 0078 surf.coord=c; 0079 else 0080 n10=floor(n/10); 0081 v=size(c,2); 0082 isnum=(n*v*3*4<=maxmem); 0083 if isnum 0084 surf.coord=zeros(n,v,3,'single'); 0085 surf.coord(1,:,:)=single(c)'; 0086 else 0087 if nargin<4 | isempty(dirname) 0088 [PATHSTR,NAME,EXT]=fileparts(filenames{1,1}); 0089 dirname=fullfile(PATHSTR,'SurfStat'); 0090 end 0091 if ~exist(dirname,'dir') 0092 [SUCCESS,MESSAGE,MESSAGEID]=mkdir(dirname); 0093 if ~SUCCESS 0094 error(MESSAGEID,['Tried to make directory ' dirname ' for memory mapping. \n',... 0095 'Please specify the name of a directory where you have write permission \n',... 0096 'as the fourth parameter of SurfStatReadSurf - see the help :-) Error: \n',... 0097 MESSAGE]); 0098 end 0099 end 0100 [PATHSTR,NAME,EXT]=fileparts(tempname); 0101 Filename=fullfile(dirname,NAME); 0102 fid=fopen(Filename,'wb'); 0103 fwrite(fid,c','single'); 0104 end 0105 v2=cumsum(vs); 0106 v1=v2-vs+1; 0107 for i=2:n 0108 if rem(i,n10)==0 0109 fprintf(1,'%s',[num2str(round(100*(1-i/n))) ' ']); 0110 end 0111 for j=1:k 0112 [s,ab]=SurfStatReadSurf1(filenames{i,j},ab,1); 0113 if isnum 0114 surf.coord(i,v1(j):v2(j),:)=single(s.coord)'; 0115 else 0116 c(:,v1(j):v2(j))=single(s.coord); 0117 end 0118 end 0119 if ~isnum 0120 fwrite(fid,c','single'); 0121 end 0122 end 0123 fprintf(1,'%s\n','Done'); 0124 if ~isnum 0125 fclose(fid); 0126 surf.coord=memmapfile(Filename,'Format',{'single' [v 3 n] 'Data'},'Writable',true); 0127 surf.coord 0128 end 0129 end 0130 0131 return 0132 end 0133