Home > marsbar > mars_utils.m

mars_utils

PURPOSE ^

collection of useful utility functions for MarsBaR etc

SYNOPSIS ^

function varargout=mars_utils(varargin)

DESCRIPTION ^

 collection of useful utility functions for MarsBaR etc
 
 fname = mars_utils('str2fname', str)
    accepts string, attempts return of string for valid filename
    The passed string should be without path or extension

 tf = mars_utils('is_valid_varname', str)
    accepts string, tests if it is a valid variable name
    returns 1 for yes.

 P = mars_utils('get_img_name', fname, flags);
    gets name of image, if image exists, checks for overwrite  
    returns filename, or empty string if overwrite not wanted

 XYZ = mars_utils('e2xyz', els, dims);
    takes element numbers in an image (e.g. find(img>5)) and the
    dimensions of the image [X Y Z] and returns the 3xN voxel
    coordinates corresponding to the N elements

 absf = mars_utils('isabspath', path);
    Takes path name, and returns flag, 1 if path is absolute on this
    system, 0 if relative or empty
 
 mars_utils('graphic_text', strs, [title_str, [, figure_str]])
    Displays cell array of text (strs) in SPM graphics window 
    with optional title (char) title_string.  Can also set to display to
    other figure window (string or figure handle).

 mars_utils('spm_version', [check_global])
    Gets SPM version string.  Robust version to allow there to be
    versions of the spm.m file to not have Contents.m files in the same
    directory.  If optional flag check_global is zero, does _not_ check
    global SPM_VER variable for version, and tries to get directly from
    spm.m / Contents.m pairs.
 
 tf = mars_utils('is_swapped_wrong', V)
    Deprecated - see version in mars_vol_utils

 tf = mars_utils('version_less_than', v1, v2)
    Returns 1 if version v1 is lower than version v2.
    Versions can be numbers.  If strings, versions can contain more than
    one '.' character. For example, '0.38.1' is less than '0.39', or
    '0.38.2'

 $Id$

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function varargout=mars_utils(varargin)
0002 % collection of useful utility functions for MarsBaR etc
0003 %
0004 % fname = mars_utils('str2fname', str)
0005 %    accepts string, attempts return of string for valid filename
0006 %    The passed string should be without path or extension
0007 %
0008 % tf = mars_utils('is_valid_varname', str)
0009 %    accepts string, tests if it is a valid variable name
0010 %    returns 1 for yes.
0011 %
0012 % P = mars_utils('get_img_name', fname, flags);
0013 %    gets name of image, if image exists, checks for overwrite
0014 %    returns filename, or empty string if overwrite not wanted
0015 %
0016 % XYZ = mars_utils('e2xyz', els, dims);
0017 %    takes element numbers in an image (e.g. find(img>5)) and the
0018 %    dimensions of the image [X Y Z] and returns the 3xN voxel
0019 %    coordinates corresponding to the N elements
0020 %
0021 % absf = mars_utils('isabspath', path);
0022 %    Takes path name, and returns flag, 1 if path is absolute on this
0023 %    system, 0 if relative or empty
0024 %
0025 % mars_utils('graphic_text', strs, [title_str, [, figure_str]])
0026 %    Displays cell array of text (strs) in SPM graphics window
0027 %    with optional title (char) title_string.  Can also set to display to
0028 %    other figure window (string or figure handle).
0029 %
0030 % mars_utils('spm_version', [check_global])
0031 %    Gets SPM version string.  Robust version to allow there to be
0032 %    versions of the spm.m file to not have Contents.m files in the same
0033 %    directory.  If optional flag check_global is zero, does _not_ check
0034 %    global SPM_VER variable for version, and tries to get directly from
0035 %    spm.m / Contents.m pairs.
0036 %
0037 % tf = mars_utils('is_swapped_wrong', V)
0038 %    Deprecated - see version in mars_vol_utils
0039 %
0040 % tf = mars_utils('version_less_than', v1, v2)
0041 %    Returns 1 if version v1 is lower than version v2.
0042 %    Versions can be numbers.  If strings, versions can contain more than
0043 %    one '.' character. For example, '0.38.1' is less than '0.39', or
0044 %    '0.38.2'
0045 %
0046 % $Id$
0047 
0048 if nargin < 1
0049   error('Need action');
0050 end
0051 Action = lower(varargin{1});
0052 
0053 switch(Action)
0054   
0055 %=======================================================================
0056 case 'str2fname'                                   %-string to file name
0057 %=======================================================================
0058 if nargin < 2
0059   error('Need to specify string');
0060 end
0061 str = varargin{2};
0062 % forbidden chars in file name
0063 badchars = unique([filesep '/\ :;.''"~*?<>|&']);
0064 
0065 tmp = find(ismember(str, badchars));   
0066 if ~isempty(tmp)
0067   str(tmp) = '_';
0068   dt = diff(tmp);
0069   if ~isempty(dt)
0070     str(tmp(dt==1))=[];
0071   end
0072 end
0073 varargout={str};
0074  
0075 %=======================================================================
0076 case 'is_valid_varname'        %- tests if string is valid variable name
0077 %=======================================================================
0078 if nargin < 2
0079   error('Need to specify string');
0080 end
0081 str = varargin{2};
0082 try 
0083   eval([str '= [];']);
0084   varargout={1};
0085 catch
0086   varargout = {0};
0087 end
0088 
0089 %=======================================================================
0090 case 'get_img_name'          %-gets name of image, checks for overwrite
0091 %=======================================================================
0092 if nargin < 2
0093   fname = '';
0094 else
0095   fname = varargin{2};
0096 end
0097 if nargin < 3
0098   flags = '';
0099 else 
0100   flags = varargin{3};
0101 end
0102 if isempty(flags)
0103   flags = 'k';
0104 end
0105 
0106 varargout = {''};
0107 fdir = spm_get(-1, '', 'Directory to save image');
0108 fname = spm_input('Image filename', '+1', 's', fname);
0109 if isempty(fname), return, end
0110 
0111 % set img extension and make absolute path
0112 [pn fn ext] = fileparts(fname);
0113 out_ext = mars_veropts('pref_img_out_ext');
0114 fname = fullfile(fdir, [fn out_ext]);
0115 fname = spm_get('cpath', fname);
0116 
0117 if any(flags == 'k') && exist(fname, 'file')
0118   if ~spm_input(['Overwrite ' fn], '+1', ...
0119         'b','Yes|No',[1 0], 1)
0120     return
0121   end
0122 end
0123 varargout = {fname};
0124 
0125 %=======================================================================
0126 case 'e2xyz'         %-returns XYZ voxel coordinates for element numbers
0127 %=======================================================================
0128 if nargin < 2
0129   error('Need element numbers');
0130 end
0131 if nargin < 3
0132   error('Need image dimensions');
0133 end
0134 els = varargin{2};
0135 dim = varargin{3};
0136 if size(els, 2) == 1, els = els'; end
0137 nz = els-1;
0138 pl_sz = dim(1)*dim(2);
0139 Z = floor(nz / pl_sz);
0140 nz = nz - Z*pl_sz;
0141 Y = floor(nz / dim(1));
0142 X = nz - Y*dim(1);
0143 XYZ = [X; Y; Z] +1;
0144 varargout = {XYZ};
0145 
0146 %=======================================================================
0147 case 'isabspath' % Returns 1 for absolute path, 0 if relative (or empty)
0148 %=======================================================================
0149 if nargin < 2
0150   error('Need path to test');
0151 end
0152 pn = varargin{2};
0153 switch (spm_platform('filesys'))
0154  case 'unx'
0155   if (~isempty(pn) && pn(1)=='/'), absf=1; else, absf=0; end
0156  case 'win'
0157   if (length(pn)>1 && pn(2)==':'), absf=1; else, absf=0; end
0158  otherwise
0159   error('isabspath not coded for this filesystem');
0160 end
0161 varargout = {absf};
0162 
0163 %=======================================================================
0164 case 'graphic_text'                 % Displays text in SPM figure window
0165 %=======================================================================
0166 if nargin < 2, error('Need text to show'); else S = varargin{2}; end
0167 if ischar(S), S = cellstr(S); end
0168 if nargin < 3, TTitle = ''; else TTitle = varargin{3}; end
0169 if nargin < 4, F = []; else, F=varargin{4}; end
0170 if isempty(F), F='Graphics'; end
0171 if ischar(F), F = spm_figure('GetWin', F); end
0172 if isempty(F), F = spm_figure('GetWin', 'Graphics'); end
0173 
0174 FS = spm('FontSizes');
0175 PF = spm_platform('fonts');
0176 
0177 spm_figure('clear', F);
0178 figure(F);
0179 hAxes = axes('Position',[0.028,0.05,0.85,0.85],...
0180         'DefaultTextInterpreter','none',...
0181         'Units','Points','Visible','off');
0182 AxPos = get(hAxes,'Position'); set(hAxes,'YLim',[0,AxPos(4)])
0183 
0184 dy = FS(10)*1.2; y0 = floor(AxPos(4)) -dy; y  = y0;
0185 
0186 text(-0.03,y0,TTitle,'FontSize',FS(14),'FontWeight','bold');
0187 y     = y0 - FS(14);
0188 
0189 %-Loop over lines of text
0190 %------------------------
0191 for i = 1:prod(size(S))
0192   d = S{i};
0193   
0194   %-For some reason, '|' characters cause a CR.
0195   d = strrep(d,'|','I');
0196   h = text(0,y,d,'FontName',PF.courier,'FontSize',FS(10));
0197   y = y - dy;
0198 end
0199  
0200 %=======================================================================
0201 case 'spm_version'                   % Robust get for SPM version string
0202 %=======================================================================
0203 % The problem we are trying to solve here is how to take account of the
0204 % unusual situation of the spm.m file _not_ being in the same directory
0205 % as the Contents.m file.  SPM8 raises an error, SPM5 and probably
0206 % previous just returns 'SPM'.  We raise an error always.
0207 
0208 try
0209     ver = spm('ver');
0210 catch
0211     err = lasterr;
0212     if strcmp('Undefined', err(1:length('Undefined')))
0213         error('Marsbar needs SPM on the matlab path')
0214     end
0215     error(lasterr)
0216 end
0217 % If version find failed, for SPM5, routine returns 'SPM'
0218 str = ['Can''t obtain SPM Revision information. Is spm.m in the same' ...
0219        ' directory as Contents.m?'];
0220 if strcmp(ver, 'SPM')
0221     error(str)
0222 end
0223 varargout = {ver};
0224 
0225 %=======================================================================
0226 case 'is_swapped_wrong'    % Returns 1 for if vol is incorrectly swapped
0227 %=======================================================================
0228 warning(...
0229     sprintf(['mars_utils version of ''is_swapped_wrong'' deprecated\n',...
0230          'Please use same function in mars_vol_utils']));
0231 varargout = {mars_vol_utils(varargin{:})};
0232 
0233 %=======================================================================
0234 case 'version_less_than' % Returns 1 for if first version less than last
0235 %=======================================================================
0236 
0237 if nargin < 3
0238   error('Need two version strings to compare');
0239 end
0240 varargout{1} = sf_ver_lt(varargin{2:3});
0241 
0242 otherwise
0243   error([Action ' is too strange']);
0244 end
0245 return
0246 
0247 % Subfunctions
0248 
0249 function tf = sf_ver_lt(v1, v2)
0250 % Returns 1 if version string v1 is lower than v2
0251 vs = {v1, v2};
0252 for v = 1:2
0253   ns = vs{v};
0254   if ~ischar(ns), ns = num2str(ns); end
0255   ns = sf_str_to_nums(ns); 
0256   cmp_mat(v, 1:length(ns)) = ns;
0257 end
0258 dp = diff(cmp_mat) * -1;
0259 for i = 1:length(dp)
0260   if dp(i)<0, tf = 1; return, end
0261   if dp(i)>0, tf = 0; return, end 
0262 end
0263 tf = 0;
0264 return
0265 
0266 function ns = sf_str_to_nums(str)
0267 str = str(ismember(str, '0123456789.'));
0268 str = ['.' str '.'];
0269 ps = find(str == '.');
0270 ns = [];
0271 for p = 2:length(ps)
0272   s = str(ps(p-1)+1:ps(p)-1);
0273   ns = [ns str2num(s)];
0274 end
0275 return

Generated on Wed 11-May-2022 16:26:09 by m2html © 2003-2019