Home > rest_20090422 > rest_misc.m

rest_misc

PURPOSE ^

Misc functions set for REST

SYNOPSIS ^

function varargout=rest_misc(AOperation, varargin)

DESCRIPTION ^

Misc functions set for REST
------------------------------------------------------------------------------------------------------------------------------
    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
 20070609
------------------------------------------------------------------------------------------------------------------------------
     Mail to Authors:  <a href="Dawnwei.Song@gmail.com">Xiaowei Song</a>; <a href="ycg.yan@gmail.com">Chaogan Yan</a> 
    Version=1.2;
    Release=20081223;
   Modified by Yan Chao-Gan 080808: also support NIFTI images.
   Last Modified by Yan Chao-Gan 081223: check the NIFTI templates.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

rest_misc.m

SOURCE CODE ^

0001 function varargout=rest_misc(AOperation, varargin)    
0002 %Misc functions set for REST
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 % 20070609
0010 %------------------------------------------------------------------------------------------------------------------------------
0011 %     Mail to Authors:  <a href="Dawnwei.Song@gmail.com">Xiaowei Song</a>; <a href="ycg.yan@gmail.com">Chaogan Yan</a>
0012 %    Version=1.2;
0013 %    Release=20081223;
0014 %   Modified by Yan Chao-Gan 080808: also support NIFTI images.
0015 %   Last Modified by Yan Chao-Gan 081223: check the NIFTI templates.
0016 
0017 switch upper(AOperation),
0018 case 'ISEXISTFIGURE',                         %IsExistFigure
0019     if nargin~=2, error('Usage: result =rest_misc( ''IsExistFigure'' , AFigureHandle);'); end    
0020     if nargin>0,
0021         varargout{1} =IsExistFigure(varargin{1});
0022     end    
0023     
0024 case 'FORCECHECKEXISTFIGURE',     %  ForceCheckExistFigure
0025     if nargin~=2, error('Usage: result =rest_misc( ''ForceCheckExistFigure'' , AFigureHandle);'); end    
0026     if nargin>0,
0027         varargout{1} =ForceCheckExistFigure(varargin{1});
0028     end    
0029     
0030 case 'COMPLAINWHYTHISOCCUR',     %ComplainWhyThisOccur
0031     if nargin~=1, error('Usage: rest_misc( ''ComplainWhyThisOccur'');'); end
0032     error(sprintf('Error call: Why did this occur?\nThere must be something wrong.\nrun "clear all" or re-start MATLAB to avoid this error.\n Dawnwei.song 20070526'));
0033 
0034 case 'REPLACESINGLEQUOTA',         % Replace single ' in filename or dirname to make sure the legal string used in sliceViewer and waveGraph
0035     if nargin~=2, error('Usage: rest_misc( ''ReplaceSingleQuota'', AFilename);'); end    
0036     AFilename =varargin{1};
0037     varargout{1} = regexprep(AFilename, '(''{1})', '''''');
0038     
0039 case 'DISPLAYLASTEXCEPTION'    , %DisplayLastException, 20070530
0040     if nargin~=1, error('Usage: rest_misc( ''DisplayLastException'');'); end
0041     if nargout>0,
0042         varargout{1} =DisplayLastException;
0043     else
0044         DisplayLastException;
0045     end
0046     
0047 case 'GETDATETIMESTR',     % GetDateTimeStr
0048     if nargin~=1, error('Usage: rest_misc( ''GetDateTimeStr'');'); end
0049     varargout{1} =GetDateTimeStr;
0050 case 'GETDATESTR',     % GetDateStr
0051     if nargin~=1, error('Usage: rest_misc( ''GetDateStr'');'); end
0052     varargout{1} =GetDateStr;    
0053     
0054 case 'GETMATLABVERSION',        %GetMatlabVersion
0055     if nargin~=1, error('Usage: rest_misc( ''GetMatlabVersion'');'); end
0056     varargout{1} =GetMatlabVersion;
0057     
0058 case 'GETCURRENTUSER',     %GetCurrentUser
0059     if nargin~=1, error('Usage: rest_misc( ''GetCurrentUser'');'); end
0060     varargout{1} =getenv('USERNAME');    % for Windows
0061     if isempty(varargout{1}),
0062         varargout{1} =getenv('USER');% for Unix ...
0063     end
0064     
0065 case 'GETRESTVERSION',         %GetRestVersion
0066     if nargin~=1, error('Usage: rest_misc( ''GetRestVersion'');'); end
0067     %if rest_misc( 'GetMatlabVersion')>=7.3,
0068         [pathstr, name, ext, versn] = fileparts(mfilename('fullpath'));
0069         theOldDir =pwd;    cd(pathstr);
0070         theHelp =help('rest');  cd(theOldDir);
0071         
0072         [posBegin, posEnd] =regexp(theHelp, 'Version=[0-9\.]+;');
0073         tmpToken =theHelp(posBegin:posEnd);
0074         [posBegin, posEnd] =regexp(tmpToken, '[0-9\.]+');
0075         theVersion =tmpToken(posBegin:posEnd);        
0076         
0077         [posBegin, posEnd] =regexp(theHelp, 'Release=[0-9\.]+;');
0078         tmpToken =theHelp(posBegin:posEnd);
0079         [posBegin, posEnd] =regexp(tmpToken, '[0-9\.]+');
0080         theRelease =tmpToken(posBegin:posEnd);
0081                 
0082         varargout{1} =theVersion;
0083         varargout{2} =theRelease;
0084     % else
0085         % varargout{1} ='1.1';
0086         % varargout{2} ='20070830';
0087     % end
0088     
0089 case 'WHEREISREST',         %WhereIsREST
0090     if nargin~=1, error('Usage: rest_misc( ''WhereIsREST'');'); end
0091     [pathstr, name, ext, versn] = fileparts(mfilename('fullpath'));
0092     varargout{1} =pathstr;
0093     
0094     
0095 % mlock locks the currently running
0096 % M-file or MEX-file in memory so that subsequent clear functions
0097 % do not remove it.Use the munlock function to return
0098 % the file to its normal, clearable state.Locking an M-file or MEX-file in memory also prevents any persistent variables defined in the file from
0099 % getting reinitialized.
0100 case 'UNLOCKMFILEINMEMORY',        %UnLockMFileInMemory
0101     if nargin~=2, error('Usage: rest_misc( ''UnLockMFileInMemory'', ''MFilename'');'); end
0102     theMFilename = varargin{1};
0103     if mislocked(theMFilename),    
0104         eval(sprintf('munlock %s', theMFilename));
0105     end
0106     
0107 case 'UNLOCKRESTFILES',         %UnlockRestFiles
0108     if nargin~=1, error('Usage: rest_misc( ''UnlockRestFiles'');'); end    
0109     munlock('rest_sliceviewer'); 
0110     munlock('rest_powerspectrum'); 
0111     clear all
0112 
0113 
0114 case 'CLEARTEMPFILES',         %ClearTempFiles, Clear temp files in temp dir generated by REST(such as fc.m, alff.m, rest_bandpass.m ...)
0115     %Suspended 20070904, dawnsong,
0116     %Todo: delete all temp direcotries before REST shut down
0117     if nargin~=1, error('Usage: rest_misc( ''ClearTempFiles'');'); end
0118     dirFCTemp     =dir(fullfile(tempdir, 'fc_*'));
0119     dirALFFTemp    =dir(fullfile(tempdir, 'ALFF_*'));
0120     dirBandPassTemp =dir(fullfile(tempdir,'BandPass_*'));
0121     
0122     for x=1:size(struct2cell(dirFCTemp),2),
0123         ans=rmdir(fullfile(tempdir, dirFCTemp(x).name), 's');%suppress the error msg
0124     end
0125     for x=1:size(struct2cell(dirALFFTemp),2),
0126         ans=rmdir(fullfile(tempdir, dirALFFTemp(x).name), 's');%suppress the error msg
0127     end
0128     for x=1:size(struct2cell(dirBandPassTemp),2),
0129         ans=rmdir(fullfile(tempdir, dirBandPassTemp(x).name), 's');%suppress the error msg
0130     end
0131     
0132     
0133 case 'CHECKTEMPLATE',         %CheckTemplate, Extract AAL & Brodmann gz file
0134     if nargin~=1, error('Usage: rest_misc( ''CheckTemplate'');'); end
0135     path =rest_misc( 'WhereIsREST');
0136     if ~(exist(fullfile(path,'Template','aal.nii'), 'file')==2), %Yan Chao-Gan 081223: check the NIFTI templates.
0137         theGZfile =fullfile(path,'Template','aal.nii.gz');
0138         if ~(exist(theGZfile, 'file')==2),
0139             error(sprintf('AAL template file %s is lost!', theGZfile));            
0140         else
0141             rest_misc( 'ExtractGZ', theGZfile, fullfile(path,'Template'));
0142         end
0143     end
0144     if ~(exist(fullfile(path,'Template','brodmann.nii'), 'file')==2), %Yan Chao-Gan 081223: check the NIFTI templates.
0145         theGZfile =fullfile(path,'Template','brodmann.nii.gz');
0146         if ~(exist(theGZfile, 'file')==2),
0147             error(sprintf('BRODMANN template file %s is lost!', theGZfile));            
0148         else
0149             rest_misc( 'ExtractGZ', theGZfile, fullfile(path,'Template'));
0150         end
0151     end
0152     if ~(exist(fullfile(path,'Template','ch2.nii'), 'file')==2), %Yan Chao-Gan 081223: check the NIFTI templates.
0153         theGZfile =fullfile(path,'Template','ch2.nii.gz');
0154         if ~(exist(theGZfile, 'file')==2),
0155             error(sprintf('BRODMANN template file %s is lost!', theGZfile));            
0156         else
0157             rest_misc( 'ExtractGZ', theGZfile, fullfile(path,'Template'));
0158         end
0159     end
0160 case 'EXTRACTGZ',    %ExtractGZ, Extract gz cmpressed file to specified dir
0161     if nargin~=3, error('Usage: rest_misc( ''ExtractGZ'', AGZFile, ADestDir);'); end
0162     AGZFile =varargin{1};
0163     ADestDir=varargin{2};
0164     gunzip(AGZFile, ADestDir);
0165     
0166 case 'EXPORTCELLS2TXT',    %ExportCells2Txt
0167     if nargin~=3, error('Usage: rest_misc( ''ExportCells2Txt'', ACellStruct, AFilename);'); end
0168     ACellStruct =varargin{1};
0169     AFilename    =varargin{2};
0170     hFile =fopen(AFilename, 'w');
0171     if hFile>0,
0172         for x=1:size(ACellStruct, 1),
0173             fprintf(hFile, '%s\r\n', ACellStruct{x, 1});
0174         end
0175         fclose(hFile);
0176     else 
0177         error(sprintf('Can''t open file: %s', AFilename));
0178     end    
0179 case 'IMPORTLINESFROMTXT',    %ImportLinesFromTxt
0180     if nargin~=2, error('Usage: [Lines]=rest_misc( ''ImportLinesFromTxt'', AFilename);'); end    
0181     AFilename    =varargin{1};
0182     varargout{1} =textread(AFilename,'%s', 'delimiter','\n');
0183     
0184 case 'VIEWROI',     %ViewROI
0185     if nargin~=2, error('Usage: rest_misc( ''ViewROI'', AROIDef);'); end    
0186     AROIDef    =varargin{1};
0187     ViewROI(AROIDef);
0188 
0189 case 'SETFIGVIEWSTYLE',        %SetFigViewStyle    %Still Need more work! 20071103
0190     if nargin~=2, error('Usage: rest_misc( ''SetFigViewStyle'', AFigHandle);'); end    
0191     AFigHandle =varargin{1};
0192     %'FontName', 'Fixed', ...
0193     SetFigViewStyle(AFigHandle);
0194 
0195 case 'ATTENTION_COORDINATES',        %Attention_Coordinates
0196     if nargin~=1, error('Usage: rest_misc( ''Attention_Coordinates'');'); end    
0197     AMsg =sprintf('Attention:\nPositive X means left and negative X means right in SliceViewer''s image!!! The coordinates you defined would be based on the origin of the EPI image, so the ORIGIN must be defined correctly before any calculation!!!');
0198     warndlg(AMsg,'Attention about coordinates in REST!!!');
0199 
0200     
0201 otherwise
0202     
0203 end
0204 
0205 function Result=IsExistFigure(AHandle)
0206     Result =0;
0207     if ~isempty(AHandle) && (AHandle>0)
0208         Result= any( allchild(0)== AHandle) ; %get(gca,'Children')
0209     end    
0210     
0211 function Result    =ForceCheckExistFigure(AHandle)
0212     Result =0;
0213     try
0214         thePos =get(AHandle, 'Position');
0215         if numel(thePos)==4,
0216             Result =1;
0217         end    
0218     catch
0219         Result =0;
0220     end
0221     
0222 function Result =DisplayLastException()
0223     theError = lasterror;
0224     Result =sprintf('\nException occured.\t(%s)\n\t%s', theError.identifier,  theError.message);            
0225     if isfield(theError, 'stack')    %Matlab 6.5 compatible
0226         for x=1:length(theError.stack)
0227             Result =sprintf('%s\n\t<a href="error:%s,%d,0">%d#line</a>,\t\t%s,\tin "%s"', ...
0228                         Result, ...
0229                         getfield(theError.stack(x,1), 'file'), ...
0230                         getfield(theError.stack(x,1), 'line'), ...
0231                         getfield(theError.stack(x,1), 'line'), ...
0232                         getfield(theError.stack(x,1), 'name'), ...
0233                         getfield(theError.stack(x,1), 'file'));            
0234         end
0235     end
0236     fprintf('%s\n', Result);    
0237     
0238 function Result=GetDateTimeStr()
0239     theDatetime=fix(clock);%[year month day hour minute seconds]
0240     Result =sprintf('%.4d%.2d%.2d_%.2d%.2d',theDatetime(1),theDatetime(2),theDatetime(3),theDatetime(4),theDatetime(5));    
0241     
0242 function Result =GetDateStr()
0243     theDatetime=fix(clock);%[year month day hour minute seconds]
0244     Result =sprintf('%.4d%.2d%.2d',theDatetime(1),theDatetime(2),theDatetime(3));    
0245 
0246     
0247 function Result=GetMatlabVersion()
0248     theVer =version;
0249     Result =str2num( theVer(1:3) );
0250     
0251 %20071103
0252 function ViewROI(AROIDef)
0253     if rest_SphereROI( 'IsBallDefinition', AROIDef)
0254         %The ROI definition is a Ball definition
0255         try
0256             [filename, pathname] = uigetfile({'*.img', 'ANALYZE or NIFTI files (*.img)';'*.nii','NIFTI files (*.nii)'}, ...
0257                                                             'Pick one brain map');
0258             if any(filename~=0) && ischar(filename) && length(filename)>4 ,    % not canceled and legal
0259                 theBrainMap =fullfile(pathname, filename);
0260                 [theOneTimePoint VoxelSize, Header] = rest_readfile(theBrainMap);
0261                 BrainSize =size(theOneTimePoint);
0262                 
0263                 [AROICenter, AROIRadius] =rest_SphereROI('STR2ROIBALL', AROIDef);
0264                 rest_SphereROI( 'BallDefinition2Mask' , AROIDef, BrainSize, VoxelSize, Header);
0265                 
0266                 theViewer =rest_sliceviewer('ShowImage', theBrainMap);
0267                 rest_sliceviewer('ShowOverlay',theViewer, fullfile(rest_misc('WhereIsREST'),'LastSphereMask'));
0268                 
0269                 %Dawnsong 20071102 Revise to make sure the left image/Right brain is +
0270                 %rest_sliceviewer('SetPhysicalPosition', theViewer, [-1, 1, 1] .* AROICenter);
0271                 
0272                 rest_sliceviewer('SetPhysicalPosition', theViewer, AROICenter);
0273             end            
0274         catch
0275             rest_misc( 'DisplayLastException');
0276         end    
0277         
0278     elseif exist(AROIDef,'file')==2    % Make sure the Definition file exist
0279         [pathstr, name, ext, versn] = fileparts(AROIDef);
0280         if strcmpi(ext, '.txt'),
0281             tmpX=load(AROIDef);
0282             if size(tmpX,2)>1,
0283                 %Average all columns to make sure tmpX only contain one column
0284                 tmpX = mean(tmpX')';
0285             end
0286             AROITimeCourse =tmpX;
0287             hFig =figure('Name', AROIDef, 'NumberTitle', 'off'); 
0288             plot(1:length(AROITimeCourse), AROITimeCourse);
0289             title(AROIDef);
0290         elseif strcmpi(ext, '.img')
0291             %The ROI definition is a mask file
0292             %maskROI =rest_loadmask(nDim1, nDim2, nDim3, AROIDef);
0293             rest_sliceviewer('ShowOverlay', AROIDef);
0294         else
0295             error(sprintf('REST doesn''t support the selected ROI definition now, Please check: \n%s', AROIDef));
0296         end
0297     else
0298         error(sprintf('Wrong ROI definition, Please check: \n%s', AROIDef));
0299     end
0300     
0301 function SetFigViewStyle(AFigHandle)
0302     theObjects =findobj(AFigHandle);
0303     for x=1:length(theObjects),
0304         theType =get(theObjects(x), 'Type');
0305         if strcmpi(theType, 'uicontrol'),
0306             % theFontName =get(theObjects(x), 'FontName');
0307             % if strcmpi(theFontName, 'default'),
0308             set(theObjects(x), 'FontName', 'FixedWidth');
0309             % end
0310         end
0311     end

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