0001 function varargout=rest_misc(AOperation, varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 switch upper(AOperation),
0018 case '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',
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',
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',
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' ,
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',
0048 if nargin~=1, error('Usage: rest_misc( ''GetDateTimeStr'');'); end
0049 varargout{1} =GetDateTimeStr;
0050 case 'GETDATESTR',
0051 if nargin~=1, error('Usage: rest_misc( ''GetDateStr'');'); end
0052 varargout{1} =GetDateStr;
0053
0054 case 'GETMATLABVERSION',
0055 if nargin~=1, error('Usage: rest_misc( ''GetMatlabVersion'');'); end
0056 varargout{1} =GetMatlabVersion;
0057
0058 case 'GETCURRENTUSER',
0059 if nargin~=1, error('Usage: rest_misc( ''GetCurrentUser'');'); end
0060 varargout{1} =getenv('USERNAME');
0061 if isempty(varargout{1}),
0062 varargout{1} =getenv('USER');
0063 end
0064
0065 case 'GETRESTVERSION',
0066 if nargin~=1, error('Usage: rest_misc( ''GetRestVersion'');'); end
0067
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
0085
0086
0087
0088
0089 case '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
0096
0097
0098
0099
0100 case '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',
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',
0115
0116
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');
0124 end
0125 for x=1:size(struct2cell(dirALFFTemp),2),
0126 ans=rmdir(fullfile(tempdir, dirALFFTemp(x).name), 's');
0127 end
0128 for x=1:size(struct2cell(dirBandPassTemp),2),
0129 ans=rmdir(fullfile(tempdir, dirBandPassTemp(x).name), 's');
0130 end
0131
0132
0133 case 'CHECKTEMPLATE',
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),
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),
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),
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',
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',
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',
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',
0185 if nargin~=2, error('Usage: rest_misc( ''ViewROI'', AROIDef);'); end
0186 AROIDef =varargin{1};
0187 ViewROI(AROIDef);
0188
0189 case 'SETFIGVIEWSTYLE',
0190 if nargin~=2, error('Usage: rest_misc( ''SetFigViewStyle'', AFigHandle);'); end
0191 AFigHandle =varargin{1};
0192
0193 SetFigViewStyle(AFigHandle);
0194
0195 case '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) ;
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')
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);
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);
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
0252 function ViewROI(AROIDef)
0253 if rest_SphereROI( 'IsBallDefinition', AROIDef)
0254
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 ,
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
0270
0271
0272 rest_sliceviewer('SetPhysicalPosition', theViewer, AROICenter);
0273 end
0274 catch
0275 rest_misc( 'DisplayLastException');
0276 end
0277
0278 elseif exist(AROIDef,'file')==2
0279 [pathstr, name, ext, versn] = fileparts(AROIDef);
0280 if strcmpi(ext, '.txt'),
0281 tmpX=load(AROIDef);
0282 if size(tmpX,2)>1,
0283
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
0292
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
0307
0308 set(theObjects(x), 'FontName', 'FixedWidth');
0309
0310 end
0311 end