Home > rest_20090422 > rest_ChangeOrigin.m

rest_ChangeOrigin

PURPOSE ^

Change the orgin of ANALYZE 7.5

SYNOPSIS ^

function rest_ChangeOrigin(ADataDir, ANewOrigin)

DESCRIPTION ^

Change the orgin of ANALYZE 7.5 
-----------------------------------------------------------
    Copyright(c) 2007~2010
    State Key Laboratory of Cognitive Neuroscience and Learning in Beijing Normal University
    Written by Xiao-Wei Song 
    http://resting-fmri.sourceforge.net
    Dawnwei.Song@gmail.com
-----------------------------------------------------------

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

rest_ChangeOrigin.m

SOURCE CODE ^

0001 function rest_ChangeOrigin(ADataDir, ANewOrigin)
0002 %Change the orgin of ANALYZE 7.5
0003 %-----------------------------------------------------------
0004 %    Copyright(c) 2007~2010
0005 %    State Key Laboratory of Cognitive Neuroscience and Learning in Beijing Normal University
0006 %    Written by Xiao-Wei Song
0007 %    http://resting-fmri.sourceforge.net
0008 %    Dawnwei.Song@gmail.com
0009 %-----------------------------------------------------------
0010     theFileList = dir(ADataDir);    
0011     ImgFileList ={};
0012     for x = 3:size(struct2cell(theFileList),2)
0013         if strcmpi(theFileList(x).name(end-3:end), '.hdr') 
0014             if strcmpi(theFileList(x).name(1:end-4), theFileList(x+1).name(1:end-4))
0015                 ImgFileList=[ImgFileList; {theFileList(x).name(1:end-4)}];
0016             else
0017                 error('*.{hdr,img} should be pairwise. Please re-examin them.');
0018             end
0019         end
0020     end
0021     clear theFileList;
0022     
0023     %read the normalized functional images
0024     % -------------------------------------------------------------------------
0025     fprintf('\n\t Read 3D EPI functional images: "%s".', ADataDir);    
0026     for x = 1:size(ImgFileList,1),            
0027         rest_waitbar(x/size(ImgFileList,1), ...
0028                     ImgFileList{x}, ...
0029                     'Reset origin','Child','NeedCancelBtn');
0030         theFilename = fullfile(ADataDir,ImgFileList{x});                
0031         % [theOneTimePoint VoxelSize, Origin] = rest_readfile(theFilename);
0032         
0033         %theNewFilename =fullfile(ADataDir,['x', ImgFileList{x}]);
0034         % rest_writefile(theOneTimePoint,theFilename,size(theOneTimePoint),VoxelSize',ANewOrigin,'int16')
0035         
0036         hdr_ChangeOrigin(theFilename, ANewOrigin);
0037         if ~mod(x,5)
0038             fprintf('.');        
0039         end
0040     end
0041     fprintf('\n\t Reset origin over: "%s".', ADataDir);    
0042     rest_waitbar;
0043     
0044     
0045 function hdr_ChangeOrigin(AFilename, ANewOrigin)
0046     if length(AFilename)>4
0047         if strcmpi(AFilename(end-3:end), '.hdr')
0048           AFilename = AFilename(1:end-4);
0049         end
0050         if strcmpi(AFilename(end-3:end), '.img')
0051           AFilename = AFilename(1:end-4);
0052         end
0053     end
0054 
0055     % open .hdr file
0056     fid = fopen([AFilename,'.hdr'],'r+');
0057     if fid > 0
0058     fseek(fid,40,'bof');
0059     dim = fread(fid,8,'int16');
0060     byteswap = 'native';
0061     % byte swapping
0062     if (dim(1) > 15 | dim(1) < 0)
0063         byteswap = 'ieee-be';
0064         fclose(fid);
0065         fid = fopen([AFilename,'.hdr'],'r','ieee-be');
0066         fseek(fid,40,'bof');
0067         dim = fread(fid,8,'int16');
0068         if (dim(1) > 15 | dim(1) < 0)
0069             byteswap = 'ieee-le';
0070             fclose(fid);
0071             fid = fopen([AFilename,'.hdr'],'r','ieee-le');
0072             fseek(fid,40,'bof');
0073             dim = fread(fid,8,'int16');
0074             if (dim(1) > 15 | dim(1) < 0)
0075                 error('Error opening header file. Dimension error');end
0076         end
0077     end
0078     else error(sprintf('Error opening header file. Please check whether the %s.hdr file exist.',filename));end
0079 
0080     %Dawnsong added, 20070904, for ROI coordinate transformation
0081     fseek(fid,148+105,'bof');
0082     Origin = fread(fid,3,'int16');
0083     %fclose(fid);
0084     
0085     %fid = fopen([AFilename,'.hdr'],'r+');
0086     fseek(fid,148+105,'bof');
0087     fwrite(fid,[ANewOrigin,0,0],'int16');
0088     fclose(fid);

Generated on Wed 29-Apr-2009 01:06:38 by m2html © 2005