From 640902a0f2d1944cc814f1864ea31f3b9dbb2d22 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 1 Mar 2012 13:54:06 -0500 Subject: [PATCH 01/48] removed some nastiness from an old merge conflict --- matlabScripts/GeneratePath.m | 3 --- 1 file changed, 3 deletions(-) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index a35c2278..2b81c546 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -56,8 +56,5 @@ end %% if statement pizza=1; -<<<<<<< HEAD -======= ->>>>>>> 5bd44d6... Updated generate path toolbox end From 77a45fc7f84925fe557895e0c500297c31785633 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 1 Mar 2012 14:43:46 -0500 Subject: [PATCH 02/48] renamed GeneratePath function, and eliminated varargin --- matlabScripts/GeneratePath.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index 2b81c546..d20d795b 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -1,4 +1,4 @@ -function OutputTemplate=generate_path_CSS(Template, varargin) +function OutputTemplate=GeneratePath(Template) index1=strfind(Template,'['); index2=strfind(Template,']'); From 47c2f72790ea619741d36422e999e6c63d0fb623 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 1 Mar 2012 14:44:34 -0500 Subject: [PATCH 03/48] moved and adapted GenerateStrings.m routine into GeneratePath.m --- matlabScripts/GeneratePath.m | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index d20d795b..11670b45 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -1,5 +1,19 @@ function OutputTemplate=GeneratePath(Template) +%% Parse Template to Identify Variables +index1=strfind(Template,'['); +index2=strfind(Template,']'); + + +%if length(index1) ~= length(index2) +% display('Your template was not contructed properly. Your open brackets and closed brackets are not balanced') +%end + +for i=1:length(index1) + VariableList{i}=Template(index1(i)+1:index2(i)-1); +end + +%% Parse String to Find Constants to fill TemplatePart index1=strfind(Template,'['); index2=strfind(Template,']'); From 657daf13080ca493aecd6c40d6ede2b8c97b7dec Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 1 Mar 2012 14:45:40 -0500 Subject: [PATCH 04/48] added some comments to explain what various lines do. Originally these were just for my benefit but might be handy for others --- matlabScripts/GeneratePath.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index 11670b45..e0095b5e 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -18,7 +18,7 @@ index2=strfind(Template,']'); if index1(1)>1 - TemplatePart{1}=Template(1:index1-1); + TemplatePart{1}=Template(1:index1-1); %Grab all of the string up to the first [ else TemplatePart{1}=''; %%% Contains everything before the first wildcard end @@ -26,10 +26,10 @@ if length(index1)==1 k=0; -else +else %Fill in all of the strings in the middle for k=1:length(index1)-1 %%% you've already gotten everything before the first index - TemplatePart{k+1}=horzcat(Template(index2(k)+1:index1(k+1)-1)); + TemplatePart{k+1}=horzcat(Template((index2(k)+1):index1(k+1)-1)); %Snag everything after the ith stop, up until the i+1th start end end From 45e8c1637e4647e6b3e45e092fffee1b1f07014e Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 1 Mar 2012 14:47:48 -0500 Subject: [PATCH 05/48] commented and updated the routine that reconstructs the path with substituted variable values. Command now uses evalin to get variable values from caller environment. --- matlabScripts/GeneratePath.m | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index e0095b5e..7b23ba00 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -40,14 +40,15 @@ TemplatePart{k+2}=''; end - +%% Reconstruct the path, piece by piece, substituting in variable values OutputTemplate =[]; -for k=1:length(varargin) +for k=1:length(VariableList) % if isnumeric(varargin{k}) % varargin{k}=num2str(varargin{k}); % end - OutputTemplate=horzcat(OutputTemplate,TemplatePart{k},varargin{k}); + VarValue = evalin('caller',VariableList{k}); + OutputTemplate=horzcat(OutputTemplate,TemplatePart{k},VarValue); %This appears to reconstruct the template without the brackets around the variables end OutputTemplate = [OutputTemplate TemplatePart{k+1}]; From 4e639712cca4a6e3c19135952fd46bf34853910b Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 1 Mar 2012 14:49:03 -0500 Subject: [PATCH 06/48] For cases where file ends in a wildcard, converted error messages to use errordlg, even though this isn't quite yet a standard and is being discussed in issue #11 --- matlabScripts/GeneratePath.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index 7b23ba00..95cd57f7 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -54,19 +54,19 @@ OutputTemplate = [OutputTemplate TemplatePart{k+1}]; - +%% Handle cases where file ends in a wildcard indexstar=strfind(OutputTemplate,'*'); if indexstar>0 %% if there is a wildcard clear filelist filelist=dir(OutputTemplate); %%% this returns the list of files matching the template. Allows wildcards. switch length(filelist) case 0 - error('Error -- did not find any files. Please look at your use of wildcards'); + errordlg('Error -- did not find any files. Please look at your use of wildcards'); case 1 [OutputDir OutputName]=fileparts(OutputTemplate); OutputTemplate = [OutputDir,filelist(1).name]; otherwise - error('Error -- found more than 1 file. Please look at your use of wildcards'); + errordlg('Error -- found more than 1 file. Please look at your use of wildcards'); end %% case staement end %% if statement pizza=1; From 09f514ecf67d26bbce12bca585e185f52721e24f Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 1 Mar 2012 14:51:08 -0500 Subject: [PATCH 07/48] added placeholders for pathchecking and path creation --- matlabScripts/GeneratePath.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index 95cd57f7..381d61b2 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -72,4 +72,8 @@ pizza=1; +%% Check if path exists (if supposed to) + +%% Make path if it doesn't exist + end From 7982bb2b6f88159533f445be7734cda4f23741f9 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 1 Mar 2012 15:18:08 -0500 Subject: [PATCH 08/48] removed older generate path functionality. This may be problematic for a number of old scripts that still call the outmoded GeneratePathCommand.m in an eval wrapper --- matlabScripts/CheckPath.m | 13 ------------- matlabScripts/GeneratePathCommand.m | 13 ------------- matlabScripts/GenerateStrings.m | 16 ---------------- 3 files changed, 42 deletions(-) delete mode 100644 matlabScripts/CheckPath.m delete mode 100644 matlabScripts/GeneratePathCommand.m delete mode 100644 matlabScripts/GenerateStrings.m diff --git a/matlabScripts/CheckPath.m b/matlabScripts/CheckPath.m deleted file mode 100644 index 9aa79aa7..00000000 --- a/matlabScripts/CheckPath.m +++ /dev/null @@ -1,13 +0,0 @@ -function CheckPath(InputPath, HelpMessage) - -if exist (InputPath) - return -else - display (sprintf('Sorry friend, I was looking for this: %s. Turns out it was not there.',InputPath)) - if exist('HelpMessage') - display (sprintf('I recommend trying this: %s',HelpMessage)) - end - display('Terminating the script now.') - error(''); -end - \ No newline at end of file diff --git a/matlabScripts/GeneratePathCommand.m b/matlabScripts/GeneratePathCommand.m deleted file mode 100644 index 620fd319..00000000 --- a/matlabScripts/GeneratePathCommand.m +++ /dev/null @@ -1,13 +0,0 @@ -function pathcallcmd=GeneratePathCommand(InputTemplate) - -VariableList=GenerateStrings(InputTemplate); -LengthList = length(VariableList); - -pathcallcmd=['GeneratePath(''',InputTemplate,'''']; %%%% to put single quote within string, put two single quotes in a row -for iVar=1:LengthList - pathcallcmd=[pathcallcmd ',' 'num2str(' VariableList{iVar} ')']; -end - -pathcallcmd=[pathcallcmd ')']; -pizza=1; -end \ No newline at end of file diff --git a/matlabScripts/GenerateStrings.m b/matlabScripts/GenerateStrings.m deleted file mode 100644 index ccd1ebef..00000000 --- a/matlabScripts/GenerateStrings.m +++ /dev/null @@ -1,16 +0,0 @@ -function OutputStrings=GenerateStrings(Template, varargin) -Template2=Template; - -index1=strfind(Template2,'['); -index2=strfind(Template2,']'); - - -%if length(index1) ~= length(index2) - % display('Your template was not contructed properly. Your open brackets and closed brackets are not balanced') -%end - -for i=1:length(index1) - OutputStrings{i}=Template2(index1(i)+1:index2(i)-1); -end - -end From da4fe09dd3a73f8eeb6cf4ca8db04cc236517b96 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 1 Mar 2012 15:39:46 -0500 Subject: [PATCH 09/48] Added help documentation to top of GeneratePath.m This should fix #22 --- matlabScripts/GeneratePath.m | 43 +++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index 381d61b2..16b3db84 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -1,4 +1,27 @@ -function OutputTemplate=GeneratePath(Template) +function OutputTemplate=GeneratePath(Template,mode) +% A tool to assist in generating paths to files and directories from user +% specified templates, with variable names filled in. +% +% FORMAT P = GeneratePath(Template,mode) +% Template String. For example +% '[Exp]/[Subject]/TASK/func/[Run]/* +% This will build a string by substituting [Exp] +% the current value of the variable Exp, etc. +% A wildcard is allowed at the end, but if it +% matches more than one file, it will generate an +% error dialog. +% +% mode String to specify the run mode. Can be... +% +% 'check' - Function will check to see if path point +% to extant file or directory, and raise error +% message for user if not. +% +% 'make' - If directory, make it (including any +% necessary parent directories). If path points to a +% file, make the containing directory, and any +% necessary parent directories. + %% Parse Template to Identify Variables index1=strfind(Template,'['); @@ -73,7 +96,21 @@ %% Check if path exists (if supposed to) +if strcmpi('check',mode) + if exist(OutPutTemplate,'file') ~= 0 + errordlg(['Error -- it appears that the file %s does not exist.' ... + 'Double check that you haven''t made a typo and that that file actually exists'],OutputTemplate) + end +end + + +%% Make path if it doesn't exist (if supposed to) +if strcmpi('make',mode) + [templatepath, templatename, templatext, templateversn] = fileparts(OutPutTemplate); + if exist(templatepath,'file') ~= 0 + mkdir(templatepath) + end +end -%% Make path if it doesn't exist - +%% End the function end From 2f4feb0eb0da7d27cefd336c46fd76140f2d2e1e Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 1 Mar 2012 16:09:25 -0500 Subject: [PATCH 10/48] Terminated an errordlg with a semi-colon. Not sure if that's necessary, but w/e --- matlabScripts/GeneratePath.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index 16b3db84..2499d6ca 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -98,8 +98,8 @@ %% Check if path exists (if supposed to) if strcmpi('check',mode) if exist(OutPutTemplate,'file') ~= 0 - errordlg(['Error -- it appears that the file %s does not exist.' ... - 'Double check that you haven''t made a typo and that that file actually exists'],OutputTemplate) + errordlg(['Error -- it appears that the file %s does not exist. ' ... + 'Double check that you haven''t made a typo and that that file actually exists'],OutputTemplate); end end From c60b79ef179672f767c83c1388f60fc9eacd8cac Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 1 Mar 2012 16:10:32 -0500 Subject: [PATCH 11/48] Added a friendly error message if make mode fails. This is another try at fix #22 inspired by @sripada --- matlabScripts/GeneratePath.m | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index 2499d6ca..46b01eb1 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -108,7 +108,12 @@ if strcmpi('make',mode) [templatepath, templatename, templatext, templateversn] = fileparts(OutPutTemplate); if exist(templatepath,'file') ~= 0 - mkdir(templatepath) + try + mkdir(templatepath) + catch + errordlg(['Error -- there was a problem generating path %s, perhaps you don''t ' ... + 'have write permissions to the directory that you specified. Confirm that you are ' ... + 'able to make the directory manually.'],templatepath); end end From 101f7a17b2719b0decaed8aac3c60934d5760bfb Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 1 Mar 2012 16:15:24 -0500 Subject: [PATCH 12/48] removed redundant lines. Good catch by @mangstad in discussion of #23 --- matlabScripts/GeneratePath.m | 3 --- 1 file changed, 3 deletions(-) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index 46b01eb1..49428eaf 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -37,9 +37,6 @@ end %% Parse String to Find Constants to fill TemplatePart -index1=strfind(Template,'['); -index2=strfind(Template,']'); - if index1(1)>1 TemplatePart{1}=Template(1:index1-1); %Grab all of the string up to the first [ else From 06de7e0c9bd4abc52b5e10820b77b7098aafa056 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 1 Mar 2012 16:20:53 -0500 Subject: [PATCH 13/48] Another good catch by @mangstad. Adjusted make mode to only attempt to make a directory if it does not already exist. --- matlabScripts/GeneratePath.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index 49428eaf..be04530e 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -104,7 +104,7 @@ %% Make path if it doesn't exist (if supposed to) if strcmpi('make',mode) [templatepath, templatename, templatext, templateversn] = fileparts(OutPutTemplate); - if exist(templatepath,'file') ~= 0 + if exist(templatepath,'file') == 0 try mkdir(templatepath) catch From e41256d16018c978efbad808c91775d15521be93 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 1 Mar 2012 16:56:04 -0500 Subject: [PATCH 14/48] Split make mode into two modes after discussion on pull request. One will make a parent directory of a file specified by the template, the other will make literally whatever the template says. Also updated help at top of function to reflect this. --- matlabScripts/GeneratePath.m | 45 +++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index be04530e..c6917f99 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -17,10 +17,19 @@ % to extant file or directory, and raise error % message for user if not. % -% 'make' - If directory, make it (including any -% necessary parent directories). If path points to a -% file, make the containing directory, and any -% necessary parent directories. +% 'makedir' - Make the directory as specified by the +% path template exactly. Be careful in using this, as +% you could end up with directories named +% 'run_01.nii' if your path returns a pointer to what +% should be a file rather than a directory. +% +% 'makeparentdir' - Parse out the parent path by +% removing the "file" part of your path (anything +% at the end of the string that isn't terminated by a +% "/"). This is useful if GeneratePath is returning +% an absolute path to a file you're planning to make +% later, but for now you want it to make a directory +% where you can place this file. %% Parse Template to Identify Variables @@ -94,23 +103,37 @@ %% Check if path exists (if supposed to) if strcmpi('check',mode) - if exist(OutPutTemplate,'file') ~= 0 - errordlg(['Error -- it appears that the file %s does not exist. ' ... - 'Double check that you haven''t made a typo and that that file actually exists'],OutputTemplate); + if exist(OutputTemplate,'file') == 0 + errordlg(sprintf(['Error -- it appears that the file %s does not exist. ' ... + 'Double check that you haven''t made a typo and that the file actually exists'],OutputTemplate)); end end %% Make path if it doesn't exist (if supposed to) -if strcmpi('make',mode) - [templatepath, templatename, templatext, templateversn] = fileparts(OutPutTemplate); +if strcmpi('makedir',mode) + if exist(OutputTemplate,'file') == 0 + try + mkdir(OutputTemplate) + catch + errordlg(sprintf(['Error -- there was a problem generating path %s, perhaps you don''t ' ... + 'have write permissions to the directory that you specified. Confirm that you are ' ... + 'able to make the directory manually.'],templatepath)); + end + end +end + +%% Make parent path if it doesn't exist (if supposed to) +if strcmpi('makeparentdir',mode) + [templatepath, templatename, templatext, templateversn] = fileparts(OutputTemplate); if exist(templatepath,'file') == 0 try mkdir(templatepath) catch - errordlg(['Error -- there was a problem generating path %s, perhaps you don''t ' ... + errordlg(sprintf(['Error -- there was a problem generating path %s, perhaps you don''t ' ... 'have write permissions to the directory that you specified. Confirm that you are ' ... - 'able to make the directory manually.'],templatepath); + 'able to make the directory manually.'],templatepath)); + end end end From bdf183ba6f462c3c7e04394d8f96802eafb23b85 Mon Sep 17 00:00:00 2001 From: kesslerd Date: Fri, 2 Mar 2012 10:17:21 -0500 Subject: [PATCH 15/48] cleaned up wording of error messages per suggestions from @sripada in pull thread #23 --- matlabScripts/GeneratePath.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index c6917f99..24800423 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -104,7 +104,7 @@ %% Check if path exists (if supposed to) if strcmpi('check',mode) if exist(OutputTemplate,'file') == 0 - errordlg(sprintf(['Error -- it appears that the file %s does not exist. ' ... + errordlg(sprintf(['Error -- it appears that the directory or file %s does not exist. ' ... 'Double check that you haven''t made a typo and that the file actually exists'],OutputTemplate)); end end @@ -116,7 +116,7 @@ try mkdir(OutputTemplate) catch - errordlg(sprintf(['Error -- there was a problem generating path %s, perhaps you don''t ' ... + errordlg(sprintf(['Error -- there was a problem writing the file/directory %s, perhaps you don''t ' ... 'have write permissions to the directory that you specified. Confirm that you are ' ... 'able to make the directory manually.'],templatepath)); end @@ -130,7 +130,7 @@ try mkdir(templatepath) catch - errordlg(sprintf(['Error -- there was a problem generating path %s, perhaps you don''t ' ... + errordlg(sprintf(['Error -- there was a problem making the directory %s, perhaps you don''t ' ... 'have write permissions to the directory that you specified. Confirm that you are ' ... 'able to make the directory manually.'],templatepath)); end From 944ac9e5e72444d58288bea02ab6dececae8ed42 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Fri, 2 Mar 2012 14:49:24 -0500 Subject: [PATCH 16/48] improved support for wildcards using spm_select. Turned off make modes for templates that include wildcards. Added support for suffix checking and appending. Still need a bit more work for iteratively working with multiple wildcards, but we're almost there --- matlabScripts/GeneratePath.m | 65 +++++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index 24800423..7ad37ced 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -1,4 +1,4 @@ -function OutputTemplate=GeneratePath(Template,mode) +function OutputTemplate=GeneratePath(Template,mode,suffix) % A tool to assist in generating paths to files and directories from user % specified templates, with variable names filled in. % @@ -84,18 +84,59 @@ %% Handle cases where file ends in a wildcard +wildcardflag=0; indexstar=strfind(OutputTemplate,'*'); -if indexstar>0 %% if there is a wildcard +if any(indexstar>0) %% if there is a wildcard + wildcardflag=1; clear filelist - filelist=dir(OutputTemplate); %%% this returns the list of files matching the template. Allows wildcards. - switch length(filelist) +% filelist=spm_select('filter',OutputTemplate); %%% this returns the list of files matching the template. Allows wildcards. +for index=1:length(indexstar) + startemplate=OutputTemplate; + curTemplate=OutputTemplate(1:indexstar(index)); + starmatch=dir(curTemplate); + switch length(starmatch) case 0 - errordlg('Error -- did not find any files. Please look at your use of wildcards'); + %Raise error + case 1 + curTemplate = fileparts(curTemplate); + replacedtemplate=fullfile(curTemplate,starmatch.name); + OutputTemplate(1:length(replacedtemplate)) = replacedtemplate; + otherwise + %Raise error + end +end + +filelist=dir(OutputTemplate); +switch length(filelist) + case 0 + errmsg='Error -- did not find any files. Please look at your use of wildcards'; + errordlg(errmsg); + error(errmsg) case 1 [OutputDir OutputName]=fileparts(OutputTemplate); OutputTemplate = [OutputDir,filelist(1).name]; otherwise - errordlg('Error -- found more than 1 file. Please look at your use of wildcards'); + if exist('suffix') + suffixmatch=[]; + for ifile=1:length(filelist) + substr=filelist(ifile).name(length((filelist(ifile).name-length(suffix)):length(filelist(ifile).name))); + suffixmatch(ifile)=strcmp(substr,suffix); + end + + filelist=filelist(substr); + + switch length(filelist) + case 0 + errordlg(['Error -- found no files matching your wildcard with the required suffix. ' ... + 'Please look at your use of wildcards.']) + case 1 + OutputTemplate = filelist(1).name; + otherwise + errordlg('Error -- found more than 1 file. Please look at your use of wildcards'); + end + else + errordlg('Error -- found more than 1 file. Please look at your use of wildcards'); + end end %% case staement end %% if statement pizza=1; @@ -111,7 +152,7 @@ %% Make path if it doesn't exist (if supposed to) -if strcmpi('makedir',mode) +if all(strcmpi('makedir',mode),wildcardflag==0) if exist(OutputTemplate,'file') == 0 try mkdir(OutputTemplate) @@ -124,7 +165,7 @@ end %% Make parent path if it doesn't exist (if supposed to) -if strcmpi('makeparentdir',mode) +if all(strcmpi('makeparentdir',mode),wildcardflag==0) [templatepath, templatename, templatext, templateversn] = fileparts(OutputTemplate); if exist(templatepath,'file') == 0 try @@ -137,5 +178,13 @@ end end +%% Check if file ends with suffix. If not, append it. +if exist('suffix') + if strcmp(OutputTemplate((length(OutputTemplate)-length(suffix)+1):length(OutputTemplate)),suffix) + OutputTemplate = [OutputTemplate suffix]; + end +end + + %% End the function end From 0b4f6ac867ee29654dddae03788af1bd76bd5421 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Mon, 5 Mar 2012 13:05:42 -0500 Subject: [PATCH 17/48] Changed structure of wildcard parsing. Now it will do it one filesep delimited block at a time, and fill it in progressively with calls to dir. Some error handling needs to be improved (currently just comments to raise an error) --- matlabScripts/GeneratePath.m | 106 +++++++++++++++++++++++------------ 1 file changed, 69 insertions(+), 37 deletions(-) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index 7ad37ced..30be7da8 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -82,64 +82,96 @@ OutputTemplate = [OutputTemplate TemplatePart{k+1}]; - -%% Handle cases where file ends in a wildcard +%% Handle cases with wildcards wildcardflag=0; indexstar=strfind(OutputTemplate,'*'); -if any(indexstar>0) %% if there is a wildcard +indexsep=strfind(OutputTemplate,filesep); %return indices of file separators +if any(indexstar>0) %% if there are any wildcards present wildcardflag=1; - clear filelist -% filelist=spm_select('filter',OutputTemplate); %%% this returns the list of files matching the template. Allows wildcards. -for index=1:length(indexstar) - startemplate=OutputTemplate; - curTemplate=OutputTemplate(1:indexstar(index)); - starmatch=dir(curTemplate); - switch length(starmatch) - case 0 - %Raise error - case 1 - curTemplate = fileparts(curTemplate); - replacedtemplate=fullfile(curTemplate,starmatch.name); - OutputTemplate(1:length(replacedtemplate)) = replacedtemplate; - otherwise - %Raise error + for index=2:length(indexsep) %run over all directory names + indexsep=strfind(OutputTemplate,filesep); %update indices of separators (they might move around after substitution, but number will not change) + prePath = OutputTemplate(1:(indexsep(index)-1)); + postPath = OutputTemplate(indexsep(index):end); + + if any(strfind(prePath,'*')>0) %if any wildcards exist in present chunk + starmatch=dir(prePath); + switch length(starmatch) + case 0 + %Raise error + case 1 + preParent = fileparts (prePath) ; + preMatch = starmatch.name ; + prePath=fullfile(preParent,preMatch) ; + OutputTemplate = [prePath postPath] ; + otherwise + %if filling in final wildcard, use suffix to clean things up + if finalflag==1 && exist('suffix') + suffixmatch=[]; + for ifile=1:length(starmatch) + substr=starmatch(ifile).name(length((starmatch(ifile).name-length(suffix)):length(starmatch(ifile).name))); + suffixmatch(ifile)=strcmp(substr,suffix); + end + + starmatch=starmatch(substr); + + switch length(starmatch) + case 0 + errordlg(['Error -- found no files matching your wildcard with the required suffix. ' ... + 'Please look at your use of wildcards.']) + case 1 + OutputTemplate = starmatch(1).name; + otherwise + errordlg('Error -- found more than 1 file. Please look at your use of wildcards'); + end + else + errordlg('Error -- found more than 1 file. Please look at your use of wildcards'); + end + end + end end end -filelist=dir(OutputTemplate); -switch length(filelist) +% handle the last piece +if any(strfind(OutputTemplate,'*')>0) %if any wildcards STILL exist (they must be in a file spec at the end of OutputTemplate) + starmatch=dir(OutputTemplate); + switch length(starmatch) case 0 - errmsg='Error -- did not find any files. Please look at your use of wildcards'; - errordlg(errmsg); - error(errmsg) + %Raise error case 1 - [OutputDir OutputName]=fileparts(OutputTemplate); - OutputTemplate = [OutputDir,filelist(1).name]; + Parent = fileparts (OutputTemplate) ; + Match = starmatch.name ; + OutputTemplate=fullfile(Parent,Match) ; otherwise + %if filling in final wildcard, use suffix to clean things up if exist('suffix') suffixmatch=[]; - for ifile=1:length(filelist) - substr=filelist(ifile).name(length((filelist(ifile).name-length(suffix)):length(filelist(ifile).name))); + for ifile=1:length(starmatch) + substr=starmatch(ifile).name(length((starmatch(ifile).name-length(suffix)):end)); suffixmatch(ifile)=strcmp(substr,suffix); end - filelist=filelist(substr); + starmatch=starmatch(substr); - switch length(filelist) + switch length(starmatch) case 0 errordlg(['Error -- found no files matching your wildcard with the required suffix. ' ... 'Please look at your use of wildcards.']) case 1 - OutputTemplate = filelist(1).name; + Parent = fileparts (OutputTemplate) ; + Match = starmatch.name ; + OutputTemplate=fullfile(Parent,Match) ; otherwise - errordlg('Error -- found more than 1 file. Please look at your use of wildcards'); + errordlg('Error -- found more than 1 file matching your suffix and wildcards. Please look at your use of wildcards'); end else errordlg('Error -- found more than 1 file. Please look at your use of wildcards'); end - end %% case staement -end %% if statement -pizza=1; + end +end + + + + %% Check if path exists (if supposed to) @@ -149,10 +181,10 @@ 'Double check that you haven''t made a typo and that the file actually exists'],OutputTemplate)); end end - + %% Make path if it doesn't exist (if supposed to) -if all(strcmpi('makedir',mode),wildcardflag==0) +if strcmpi('makedir',mode) && wildcardflag==0 if exist(OutputTemplate,'file') == 0 try mkdir(OutputTemplate) @@ -165,7 +197,7 @@ end %% Make parent path if it doesn't exist (if supposed to) -if all(strcmpi('makeparentdir',mode),wildcardflag==0) +if strcmpi('makeparentdir',mode) && wildcardflag==0 [templatepath, templatename, templatext, templateversn] = fileparts(OutputTemplate); if exist(templatepath,'file') == 0 try From d3b5b2b4f8cdf8a3acde4bbf8ab7a66b2bfcfd95 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Mon, 5 Mar 2012 14:51:44 -0500 Subject: [PATCH 18/48] tested error messages and handling. things looking good --- matlabScripts/GeneratePath.m | 127 ++++++++++++++++++++++------------- 1 file changed, 79 insertions(+), 48 deletions(-) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index 30be7da8..7913929c 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -1,8 +1,8 @@ function OutputTemplate=GeneratePath(Template,mode,suffix) % A tool to assist in generating paths to files and directories from user % specified templates, with variable names filled in. -% -% FORMAT P = GeneratePath(Template,mode) +% +% FORMAT P = GeneratePath(Template,mode,suffix) % Template String. For example % '[Exp]/[Subject]/TASK/func/[Run]/* % This will build a string by substituting [Exp] @@ -10,26 +10,44 @@ % A wildcard is allowed at the end, but if it % matches more than one file, it will generate an % error dialog. -% +% % mode String to specify the run mode. Can be... -% +% % 'check' - Function will check to see if path point % to extant file or directory, and raise error % message for user if not. -% +% % 'makedir' - Make the directory as specified by the % path template exactly. Be careful in using this, as % you could end up with directories named % 'run_01.nii' if your path returns a pointer to what -% should be a file rather than a directory. -% +% should be a file rather than a directory. If +% Template includes any wildcard this mode will be +% disabled. +% % 'makeparentdir' - Parse out the parent path by % removing the "file" part of your path (anything % at the end of the string that isn't terminated by a % "/"). This is useful if GeneratePath is returning % an absolute path to a file you're planning to make % later, but for now you want it to make a directory -% where you can place this file. +% where you can place this file. If Template includes +% any wildcards this mode will be disabled. +% +% 'null' (or anything else). Include some meaningless +% string here to pad the arguments in if you wish to +% use later arguments. +% +% suffix If you expect the final resolution of the call to +% include be a particular suffix (e.g. .nii) indicate +% it in this slot. This will use the suffix to +% whittle down the list of potential matches when +% working with wildcards, and, if the final result +% does not end in the specified suffix, it will be +% appended. +% +% ------------------------------------------------- +% Note: Wildcards are incompatible with both %% Parse Template to Identify Variables @@ -94,38 +112,29 @@ postPath = OutputTemplate(indexsep(index):end); if any(strfind(prePath,'*')>0) %if any wildcards exist in present chunk + [preParent, preWild, preExt] = fileparts(prePath); + preWild = [preWild preExt]; starmatch=dir(prePath); switch length(starmatch) case 0 - %Raise error + %Raise error CHECKED + errormsg = sprintf(['Error -- No subdirectories found in "%s" that match your wildcard expression "%s". ' ... + 'Please check your use of wildcards.'], ... + preParent, preWild); + errordlg(errormsg,'Path Generation Error') + error(errormsg) case 1 preParent = fileparts (prePath) ; preMatch = starmatch.name ; prePath=fullfile(preParent,preMatch) ; OutputTemplate = [prePath postPath] ; otherwise - %if filling in final wildcard, use suffix to clean things up - if finalflag==1 && exist('suffix') - suffixmatch=[]; - for ifile=1:length(starmatch) - substr=starmatch(ifile).name(length((starmatch(ifile).name-length(suffix)):length(starmatch(ifile).name))); - suffixmatch(ifile)=strcmp(substr,suffix); - end - - starmatch=starmatch(substr); - - switch length(starmatch) - case 0 - errordlg(['Error -- found no files matching your wildcard with the required suffix. ' ... - 'Please look at your use of wildcards.']) - case 1 - OutputTemplate = starmatch(1).name; - otherwise - errordlg('Error -- found more than 1 file. Please look at your use of wildcards'); - end - else - errordlg('Error -- found more than 1 file. Please look at your use of wildcards'); - end + %CHECKED + errormsg = sprintf(['Error -- More than one subdirectory found in "%s" matches your wildcard expression "%s". ' ... + 'Please check your use of wildcards.'], ... + preParent, preWild); + errordlg(errormsg) + error(errormsg) end end end @@ -134,64 +143,84 @@ % handle the last piece if any(strfind(OutputTemplate,'*')>0) %if any wildcards STILL exist (they must be in a file spec at the end of OutputTemplate) starmatch=dir(OutputTemplate); + [preParent, preWild, preExt] = fileparts(OutputTemplate); + preWild = [preWild preExt]; switch length(starmatch) case 0 - %Raise error + %Raise error CHECKED + errormsg = sprintf(['Error -- No files found in "%s" that match your wildcard expression "%s". ' ... + 'Please check your use of wildcards.'], ... + preParent, preWild); + errordlg(errormsg) + error(errormsg) case 1 Parent = fileparts (OutputTemplate) ; Match = starmatch.name ; OutputTemplate=fullfile(Parent,Match) ; otherwise - %if filling in final wildcard, use suffix to clean things up if exist('suffix') suffixmatch=[]; for ifile=1:length(starmatch) - substr=starmatch(ifile).name(length((starmatch(ifile).name-length(suffix)):end)); + substr=starmatch(ifile).name((length(starmatch(ifile).name)-length(suffix)+1):end); suffixmatch(ifile)=strcmp(substr,suffix); end - starmatch=starmatch(substr); + starmatch=starmatch(find(suffixmatch)); switch length(starmatch) case 0 - errordlg(['Error -- found no files matching your wildcard with the required suffix. ' ... - 'Please look at your use of wildcards.']) + errormsg=sprintf(['Error -- found no files in "%s" matching your wildcard "%s" with the required suffix "%s". ' ... + 'Please look at your use of wildcards.'], ... + preParent,preWild,suffix); + errordlg(errormsg) + error(errormsg) case 1 Parent = fileparts (OutputTemplate) ; Match = starmatch.name ; OutputTemplate=fullfile(Parent,Match) ; otherwise - errordlg('Error -- found more than 1 file matching your suffix and wildcards. Please look at your use of wildcards'); + errormsg = sprintf(['Error -- More than one file found in "%s" matches your wildcard expression "%s" ' ... + 'and required suffix "%s". Please check your use of wildcards.'], ... + preParent, preWild, suffix); + errordlg(errormsg) + error(errormsg) end else - errordlg('Error -- found more than 1 file. Please look at your use of wildcards'); + %Checked + errormsg = sprintf(['More than one file found in "%s" matches your wildcard expression "%s". ' ... + 'Please check your use of wildcards.'], ... + preParent, preWild); + errordlg(errormsg) + error(errormsg) end end end - - - + + + %% Check if path exists (if supposed to) if strcmpi('check',mode) if exist(OutputTemplate,'file') == 0 - errordlg(sprintf(['Error -- it appears that the directory or file %s does not exist. ' ... + errordlg(sprintf(['Error -- it appears that the directory or file "%s" does not exist. ' ... 'Double check that you haven''t made a typo and that the file actually exists'],OutputTemplate)); end end - + %% Make path if it doesn't exist (if supposed to) if strcmpi('makedir',mode) && wildcardflag==0 if exist(OutputTemplate,'file') == 0 try mkdir(OutputTemplate) catch - errordlg(sprintf(['Error -- there was a problem writing the file/directory %s, perhaps you don''t ' ... + errormsg=sprintf(['Error -- there was a problem writing the file/directory "%s", perhaps you don''t ' ... 'have write permissions to the directory that you specified. Confirm that you are ' ... - 'able to make the directory manually.'],templatepath)); + 'able to make the directory manually.'],templatepath); + errordlg(errormsg); + error(errormsg); end end end @@ -203,9 +232,11 @@ try mkdir(templatepath) catch - errordlg(sprintf(['Error -- there was a problem making the directory %s, perhaps you don''t ' ... + errordmsg=sprintf(['Error -- there was a problem making the directory "%s", perhaps you don''t ' ... 'have write permissions to the directory that you specified. Confirm that you are ' ... - 'able to make the directory manually.'],templatepath)); + 'able to make the directory manually.'],templatepath); + errordlg(errormsg); + error(errormsg); end end end From b4306afc24eca3ab751ed3a94f1d731eeb148cfd Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Mon, 5 Mar 2012 15:53:00 -0500 Subject: [PATCH 19/48] Added support for passing a structure object with options to GeneratePath, and cleaned up lots of features. --- matlabScripts/GeneratePath.m | 92 ++++++++++++++++++++++++++---------- 1 file changed, 68 insertions(+), 24 deletions(-) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index 7913929c..13bfbf56 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -1,17 +1,21 @@ -function OutputTemplate=GeneratePath(Template,mode,suffix) +function OutputTemplate=GeneratePath(Template) % A tool to assist in generating paths to files and directories from user % specified templates, with variable names filled in. % -% FORMAT P = GeneratePath(Template,mode,suffix) -% Template String. For example -% '[Exp]/[Subject]/TASK/func/[Run]/* +% FORMAT P = GeneratePath(STRUCT) +% In this format, a STRUCT(ure) object is passed to GeneratePath. +% +% STRUCT The struct object contains fields for various +% settings to be communicated to GeneratePath +% +% STRUCT.Template REQUIRED - String. For example +% '[Exp]/[Subject]/TASK/func/[Run]/' % This will build a string by substituting [Exp] -% the current value of the variable Exp, etc. -% A wildcard is allowed at the end, but if it -% matches more than one file, it will generate an -% error dialog. +% with the current value of the variable Exp, etc. +% Wildcards are allowed throughout to signify none, +% one, or many characters. % -% mode String to specify the run mode. Can be... +% STRUCT.mode Optional - String to specify the run mode. Can be... % % 'check' - Function will check to see if path point % to extant file or directory, and raise error @@ -34,11 +38,11 @@ % where you can place this file. If Template includes % any wildcards this mode will be disabled. % -% 'null' (or anything else). Include some meaningless -% string here to pad the arguments in if you wish to -% use later arguments. +% NOTE - If STRUCT.Template includes any wildcards, +% both makedir and makeparentdir modes will be +% disabled. % -% suffix If you expect the final resolution of the call to +% STRUCT.suffix Optional - If you expect the final resolution of the call to % include be a particular suffix (e.g. .nii) indicate % it in this slot. This will use the suffix to % whittle down the list of potential matches when @@ -46,9 +50,46 @@ % does not end in the specified suffix, it will be % appended. % +% STRUCT.type Numeric. Can be... +% +% 1 - STRUCT.Template should resolve to a DIRECTORY +% path. This will influence appropriateness of error +% messages when failing to find a particular path. +% +% +% +% +% FORMAT P = GeneratePath(Template) +% In this simplified format, GeneratePath is passed simply a string. +% +% Template String. For example +% '[Exp]/[Subject]/TASK/func/[Run]/* +% This will build a string by substituting [Exp] +% the current value of the variable Exp, etc. +% A wildcard is allowed at the end, but if it +% matches more than one file, it will generate an +% error dialog. +% +% % ------------------------------------------------- -% Note: Wildcards are incompatible with both +%% Parse arguments + +type=0; %default to 0 + +if(isstruct(Template)) + if(isfield(Template,'mode')) mode=Template.mode; end; + if(isfield(Template,'suffix')) suffix=Template.suffix; end; + if(isfield(Template,'type')) type=Template.type; end; + Template = Template.Template; +end + +%% Clean up template based on type +if(type==1) + if(~strcmpi(Template(end),filesep)) + Template = [Template filesep]; + end +end %% Parse Template to Identify Variables index1=strfind(Template,'['); @@ -133,7 +174,7 @@ errormsg = sprintf(['Error -- More than one subdirectory found in "%s" matches your wildcard expression "%s". ' ... 'Please check your use of wildcards.'], ... preParent, preWild); - errordlg(errormsg) + errordlg(errormsg,'Path Generation Error') error(errormsg) end end @@ -151,7 +192,7 @@ errormsg = sprintf(['Error -- No files found in "%s" that match your wildcard expression "%s". ' ... 'Please check your use of wildcards.'], ... preParent, preWild); - errordlg(errormsg) + errordlg(errormsg,'Path Generation Error') error(errormsg) case 1 Parent = fileparts (OutputTemplate) ; @@ -172,7 +213,7 @@ errormsg=sprintf(['Error -- found no files in "%s" matching your wildcard "%s" with the required suffix "%s". ' ... 'Please look at your use of wildcards.'], ... preParent,preWild,suffix); - errordlg(errormsg) + errordlg(errormsg,'Path Generation Error') error(errormsg) case 1 Parent = fileparts (OutputTemplate) ; @@ -182,7 +223,7 @@ errormsg = sprintf(['Error -- More than one file found in "%s" matches your wildcard expression "%s" ' ... 'and required suffix "%s". Please check your use of wildcards.'], ... preParent, preWild, suffix); - errordlg(errormsg) + errordlg(errormsg,'Path Generation Error') error(errormsg) end else @@ -190,7 +231,7 @@ errormsg = sprintf(['More than one file found in "%s" matches your wildcard expression "%s". ' ... 'Please check your use of wildcards.'], ... preParent, preWild); - errordlg(errormsg) + errordlg(errormsg,'Path Generation Error') error(errormsg) end end @@ -204,8 +245,11 @@ %% Check if path exists (if supposed to) if strcmpi('check',mode) if exist(OutputTemplate,'file') == 0 - errordlg(sprintf(['Error -- it appears that the directory or file "%s" does not exist. ' ... - 'Double check that you haven''t made a typo and that the file actually exists'],OutputTemplate)); + errormsg = sprintf(['Error -- it appears that the directory or file "%s" does not exist. ' ... + 'Double check that you haven''t made a typo and that the file actually exists'],OutputTemplate); + errordlg(errormsg,'Path Generation Error'); + error(errormsg) + end end @@ -219,7 +263,7 @@ errormsg=sprintf(['Error -- there was a problem writing the file/directory "%s", perhaps you don''t ' ... 'have write permissions to the directory that you specified. Confirm that you are ' ... 'able to make the directory manually.'],templatepath); - errordlg(errormsg); + errordlg(errormsg,'Path Generation Error'); error(errormsg); end end @@ -235,7 +279,7 @@ errordmsg=sprintf(['Error -- there was a problem making the directory "%s", perhaps you don''t ' ... 'have write permissions to the directory that you specified. Confirm that you are ' ... 'able to make the directory manually.'],templatepath); - errordlg(errormsg); + errordlg(errormsg,'Path Generation Error'); error(errormsg); end end @@ -243,7 +287,7 @@ %% Check if file ends with suffix. If not, append it. if exist('suffix') - if strcmp(OutputTemplate((length(OutputTemplate)-length(suffix)+1):length(OutputTemplate)),suffix) + if ~strcmp(OutputTemplate((length(OutputTemplate)-length(suffix)+1):length(OutputTemplate)),suffix) OutputTemplate = [OutputTemplate suffix]; end end From 9a9b8adb1bde751f40535a871f1d48ac4c9aea13 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Mon, 5 Mar 2012 16:21:05 -0500 Subject: [PATCH 20/48] added a default setting for mode per suggestion from @mangstad --- matlabScripts/GeneratePath.m | 1 + 1 file changed, 1 insertion(+) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index 13bfbf56..d6d372fa 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -76,6 +76,7 @@ %% Parse arguments type=0; %default to 0 +mode='null'; if(isstruct(Template)) if(isfield(Template,'mode')) mode=Template.mode; end; From 0dca2680c41257d15f0c355380bc89f9e5e4958b Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Mon, 5 Mar 2012 16:26:30 -0500 Subject: [PATCH 21/48] disabled suffix checking and appending in makedir mode per @mangstad --- matlabScripts/GeneratePath.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index d6d372fa..696ce3de 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -49,6 +49,7 @@ % working with wildcards, and, if the final result % does not end in the specified suffix, it will be % appended. +% NOTE - Specifying a suffix will disable makedir mode. % % STRUCT.type Numeric. Can be... % @@ -287,7 +288,7 @@ end %% Check if file ends with suffix. If not, append it. -if exist('suffix') +if exist('suffix') && mode~='makedir' if ~strcmp(OutputTemplate((length(OutputTemplate)-length(suffix)+1):length(OutputTemplate)),suffix) OutputTemplate = [OutputTemplate suffix]; end From ee5527e6b8864f218c35067c14e244abfe21d7ae Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Mon, 5 Mar 2012 16:45:08 -0500 Subject: [PATCH 22/48] Added error messages and handling for cases where user specifies an undefined variable in brackets in their template specification --- matlabScripts/GeneratePath.m | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index 696ce3de..0c7825f7 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -97,11 +97,6 @@ index1=strfind(Template,'['); index2=strfind(Template,']'); - -%if length(index1) ~= length(index2) -% display('Your template was not contructed properly. Your open brackets and closed brackets are not balanced') -%end - for i=1:length(index1) VariableList{i}=Template(index1(i)+1:index2(i)-1); end @@ -134,10 +129,16 @@ OutputTemplate =[]; for k=1:length(VariableList) - % if isnumeric(varargin{k}) - % varargin{k}=num2str(varargin{k}); - % end - VarValue = evalin('caller',VariableList{k}); + try + VarValue = evalin('caller',VariableList{k}); + catch + errormsg = sprintf(['Error -- The variable "%s" that you enclosed in brackets does not have a ' ... + 'defined value. Double check that you have not made a typo (e.g. [EXP] instead of Exp) and carefully ' ... + 'read the commented instructions around your path template specification to be sure of which variables ' ... + 'you can use in bracketed expressions.'],VariableList{k}); + errordlg(errormsg,'Path Generation Error') + error(errormsg) + end OutputTemplate=horzcat(OutputTemplate,TemplatePart{k},VarValue); %This appears to reconstruct the template without the brackets around the variables end From dc56ce43431e699a337c2603f5635ce1d4472e34 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Tue, 6 Mar 2012 09:02:40 -0500 Subject: [PATCH 23/48] fixed string comparison to use strcmpi instead of == per @mangstad --- matlabScripts/GeneratePath.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index 0c7825f7..28f78b95 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -289,7 +289,7 @@ end %% Check if file ends with suffix. If not, append it. -if exist('suffix') && mode~='makedir' +if exist('suffix') && strcmpi('makedir',mode) if ~strcmp(OutputTemplate((length(OutputTemplate)-length(suffix)+1):length(OutputTemplate)),suffix) OutputTemplate = [OutputTemplate suffix]; end From bc01cead1336f3668d475e08cc582a7561de6e84 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Wed, 7 Mar 2012 14:17:41 -0500 Subject: [PATCH 24/48] Adjusted genpath to move suffix checking to top Also changed error messages to reference suffix mode, if enabled, to explain why their wildcard may have changed. Editing was done in emacs octave mode which has some nonstandard indent settings, so we should re-auto indent this in matlab, then recommit. Some of the "changes" are not really changes as they are just indent alterations. --- matlabScripts/GeneratePath.m | 67 ++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 38 deletions(-) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/GeneratePath.m index 28f78b95..1f3c1666 100644 --- a/matlabScripts/GeneratePath.m +++ b/matlabScripts/GeneratePath.m @@ -125,6 +125,14 @@ TemplatePart{k+2}=''; end +%% Check if file ends with suffix. If not, append it. +if exist('suffix') && strcmpi('makedir',mode) + if ~strcmp(OutputTemplate((length(OutputTemplate)-length(suffix)+1):length(OutputTemplate)),suffix) + OutputTemplate = [OutputTemplate suffix]; + end +end + + %% Reconstruct the path, piece by piece, substituting in variable values OutputTemplate =[]; @@ -161,10 +169,11 @@ starmatch=dir(prePath); switch length(starmatch) case 0 - %Raise error CHECKED - errormsg = sprintf(['Error -- No subdirectories found in "%s" that match your wildcard expression "%s". ' ... - 'Please check your use of wildcards.'], ... - preParent, preWild); + + %Raise error CHECKED + errormsg = sprintf(['Error -- No subdirectories found in "%s" that match your wildcard expression "%s". ' ... + 'Please check your use of wildcards.'], ... + preParent, preWild); errordlg(errormsg,'Path Generation Error') error(errormsg) case 1 @@ -192,43 +201,31 @@ switch length(starmatch) case 0 %Raise error CHECKED - errormsg = sprintf(['Error -- No files found in "%s" that match your wildcard expression "%s". ' ... - 'Please check your use of wildcards.'], ... - preParent, preWild); - errordlg(errormsg,'Path Generation Error') - error(errormsg) + if(exist('suffix') + errormsg=sprintf(['Error -- found no files in "%s" matching your wildcard "%s". ' ... + 'Note: the suffix "%s" may have been added to your wildcard. ' ... + 'Please look at your use of wildcards.'], ... + preParent,preWild,suffix); + errordlg(errormsg,'Path Generation Error') + error(errormsg) + else + errormsg = sprintf(['Error -- No files found in "%s" that match your wildcard expression "%s". ' ... + 'Please check your use of wildcards.'], ... + preParent, preWild); + errordlg(errormsg,'Path Generation Error') + error(errormsg) + end case 1 Parent = fileparts (OutputTemplate) ; Match = starmatch.name ; OutputTemplate=fullfile(Parent,Match) ; otherwise if exist('suffix') - suffixmatch=[]; - for ifile=1:length(starmatch) - substr=starmatch(ifile).name((length(starmatch(ifile).name)-length(suffix)+1):end); - suffixmatch(ifile)=strcmp(substr,suffix); - end - - starmatch=starmatch(find(suffixmatch)); - - switch length(starmatch) - case 0 - errormsg=sprintf(['Error -- found no files in "%s" matching your wildcard "%s" with the required suffix "%s". ' ... - 'Please look at your use of wildcards.'], ... - preParent,preWild,suffix); - errordlg(errormsg,'Path Generation Error') - error(errormsg) - case 1 - Parent = fileparts (OutputTemplate) ; - Match = starmatch.name ; - OutputTemplate=fullfile(Parent,Match) ; - otherwise - errormsg = sprintf(['Error -- More than one file found in "%s" matches your wildcard expression "%s" ' ... - 'and required suffix "%s". Please check your use of wildcards.'], ... + errormsg = sprintf(['Error -- More than one file found in "%s" matches your wildcard "%s". ' ... + 'Note: the suffix "%s" may have been added to your wildcard. Please check your use of wildcards.'], ... preParent, preWild, suffix); errordlg(errormsg,'Path Generation Error') error(errormsg) - end else %Checked errormsg = sprintf(['More than one file found in "%s" matches your wildcard expression "%s". ' ... @@ -288,12 +285,6 @@ end end -%% Check if file ends with suffix. If not, append it. -if exist('suffix') && strcmpi('makedir',mode) - if ~strcmp(OutputTemplate((length(OutputTemplate)-length(suffix)+1):length(OutputTemplate)),suffix) - OutputTemplate = [OutputTemplate suffix]; - end -end %% End the function From 61ae4a202862855213974ededf79a4b331fe7192 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Wed, 7 Mar 2012 15:19:51 -0500 Subject: [PATCH 25/48] renamed GeneratePath.m to align with new methods core naming conventions --- matlabScripts/{GeneratePath.m => mc_GenPath.m} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename matlabScripts/{GeneratePath.m => mc_GenPath.m} (100%) diff --git a/matlabScripts/GeneratePath.m b/matlabScripts/mc_GenPath.m similarity index 100% rename from matlabScripts/GeneratePath.m rename to matlabScripts/mc_GenPath.m From 33820e502f1ee5e58cfbb2582182e18fa9ead481 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Wed, 7 Mar 2012 15:20:45 -0500 Subject: [PATCH 26/48] updated indentation to be consistent with matlab IDE --- matlabScripts/mc_GenPath.m | 48 +++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index 1f3c1666..4a915d66 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -170,10 +170,10 @@ switch length(starmatch) case 0 - %Raise error CHECKED - errormsg = sprintf(['Error -- No subdirectories found in "%s" that match your wildcard expression "%s". ' ... - 'Please check your use of wildcards.'], ... - preParent, preWild); + %Raise error CHECKED + errormsg = sprintf(['Error -- No subdirectories found in "%s" that match your wildcard expression "%s". ' ... + 'Please check your use of wildcards.'], ... + preParent, preWild); errordlg(errormsg,'Path Generation Error') error(errormsg) case 1 @@ -201,31 +201,31 @@ switch length(starmatch) case 0 %Raise error CHECKED - if(exist('suffix') - errormsg=sprintf(['Error -- found no files in "%s" matching your wildcard "%s". ' ... - 'Note: the suffix "%s" may have been added to your wildcard. ' ... - 'Please look at your use of wildcards.'], ... - preParent,preWild,suffix); - errordlg(errormsg,'Path Generation Error') - error(errormsg) - else - errormsg = sprintf(['Error -- No files found in "%s" that match your wildcard expression "%s". ' ... - 'Please check your use of wildcards.'], ... - preParent, preWild); - errordlg(errormsg,'Path Generation Error') - error(errormsg) - end + if(exist('suffix') + errormsg=sprintf(['Error -- found no files in "%s" matching your wildcard "%s". ' ... + 'Note: the suffix "%s" may have been added to your wildcard. ' ... + 'Please look at your use of wildcards.'], ... + preParent,preWild,suffix); + errordlg(errormsg,'Path Generation Error') + error(errormsg) + else + errormsg = sprintf(['Error -- No files found in "%s" that match your wildcard expression "%s". ' ... + 'Please check your use of wildcards.'], ... + preParent, preWild); + errordlg(errormsg,'Path Generation Error') + error(errormsg) + end case 1 Parent = fileparts (OutputTemplate) ; Match = starmatch.name ; OutputTemplate=fullfile(Parent,Match) ; otherwise if exist('suffix') - errormsg = sprintf(['Error -- More than one file found in "%s" matches your wildcard "%s". ' ... - 'Note: the suffix "%s" may have been added to your wildcard. Please check your use of wildcards.'], ... - preParent, preWild, suffix); - errordlg(errormsg,'Path Generation Error') - error(errormsg) + errormsg = sprintf(['Error -- More than one file found in "%s" matches your wildcard "%s". ' ... + 'Note: the suffix "%s" may have been added to your wildcard. Please check your use of wildcards.'], ... + preParent, preWild, suffix); + errordlg(errormsg,'Path Generation Error') + error(errormsg) else %Checked errormsg = sprintf(['More than one file found in "%s" matches your wildcard expression "%s". ' ... @@ -249,7 +249,7 @@ 'Double check that you haven''t made a typo and that the file actually exists'],OutputTemplate); errordlg(errormsg,'Path Generation Error'); error(errormsg) - + end end From 28bfca41c28d4d25e8b11235a1d681e313781482 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 8 Mar 2012 12:34:19 -0500 Subject: [PATCH 27/48] cleaned up help section to be more specific, and updated names of functions to be consistent with renamed file --- matlabScripts/mc_GenPath.m | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index 4a915d66..77e35997 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -1,8 +1,8 @@ -function OutputTemplate=GeneratePath(Template) +function OutputTemplate=mc_GenPath(Template) % A tool to assist in generating paths to files and directories from user % specified templates, with variable names filled in. % -% FORMAT P = GeneratePath(STRUCT) +% FORMAT P = mc_GenPath(STRUCT) % In this format, a STRUCT(ure) object is passed to GeneratePath. % % STRUCT The struct object contains fields for various @@ -48,7 +48,9 @@ % whittle down the list of potential matches when % working with wildcards, and, if the final result % does not end in the specified suffix, it will be -% appended. +% appended. Suffix means suffix, not extension, so if +% you want to make sure a file ends in .nii, include +% the dot in STRUCT.suffix % NOTE - Specifying a suffix will disable makedir mode. % % STRUCT.type Numeric. Can be... @@ -60,8 +62,10 @@ % % % -% FORMAT P = GeneratePath(Template) -% In this simplified format, GeneratePath is passed simply a string. +% FORMAT P = mc_GenPath(Template) +% In this simplified format, GeneratePath is passed simply a string. All +% other options as discussed above are effectively disabled when this +% format is used. % % Template String. For example % '[Exp]/[Subject]/TASK/func/[Run]/* From 914d0b4865df52a2021a542eb4373d2032321ebf Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 8 Mar 2012 12:35:35 -0500 Subject: [PATCH 28/48] cleaned up cell division around bracket expansion --- matlabScripts/mc_GenPath.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index 77e35997..1d958e87 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -97,7 +97,8 @@ end end -%% Parse Template to Identify Variables +%% Do bracket expansion +% Parse Template to Identify Variables index1=strfind(Template,'['); index2=strfind(Template,']'); From 749cf6db1521da70b3e0a1a75060a7100b687f22 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 8 Mar 2012 12:36:57 -0500 Subject: [PATCH 29/48] move bracket expansion inside if block, so it will only occur if there are any complete brackets to expand --- matlabScripts/mc_GenPath.m | 43 ++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index 1d958e87..032462a4 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -102,32 +102,35 @@ index1=strfind(Template,'['); index2=strfind(Template,']'); -for i=1:length(index1) - VariableList{i}=Template(index1(i)+1:index2(i)-1); -end +if(any(index1>0) && any(index2>0)) -%% Parse String to Find Constants to fill TemplatePart -if index1(1)>1 - TemplatePart{1}=Template(1:index1-1); %Grab all of the string up to the first [ -else - TemplatePart{1}=''; %%% Contains everything before the first wildcard -end + for i=1:length(index1) + VariableList{i}=Template(index1(i)+1:index2(i)-1); + end + + %% Parse String to Find Constants to fill TemplatePart + if index1(1)>1 + TemplatePart{1}=Template(1:index1-1); %Grab all of the string up to the first [ + else + TemplatePart{1}=''; %%% Contains everything before the first wildcard + end -if length(index1)==1 - k=0; -else %Fill in all of the strings in the middle + if(length(index1)==1) + k=0; + else %Fill in all of the strings in the middle - for k=1:length(index1)-1 %%% you've already gotten everything before the first index - TemplatePart{k+1}=horzcat(Template((index2(k)+1):index1(k+1)-1)); %Snag everything after the ith stop, up until the i+1th start + for k=1:length(index1)-1 %%% you've already gotten everything before the first index + TemplatePart{k+1}=horzcat(Template((index2(k)+1):index1(k+1)-1)); %Snag everything after the ith stop, up until the i+1th start + end end -end -%%%% this gets the last bit of the template after the final ']' -if index2(k+1) Date: Thu, 8 Mar 2012 12:38:21 -0500 Subject: [PATCH 30/48] fix typo around suffix checking to disable if it running in makedir mode --- matlabScripts/mc_GenPath.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index 032462a4..34a9a6e7 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -134,8 +134,8 @@ end %% Check if file ends with suffix. If not, append it. -if exist('suffix') && strcmpi('makedir',mode) - if ~strcmp(OutputTemplate((length(OutputTemplate)-length(suffix)+1):length(OutputTemplate)),suffix) +if(exist('suffix') && ~strcmpi('makedir',mode)) + if(~strcmp(OutputTemplate((length(OutputTemplate)-length(suffix)+1):length(OutputTemplate)),suffix)) OutputTemplate = [OutputTemplate suffix]; end end From d0f9cbae38c654d838bb88f553536ca6aa6ef1f6 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 8 Mar 2012 12:38:59 -0500 Subject: [PATCH 31/48] standardized usage of if blocks to be enclosed in parens --- matlabScripts/mc_GenPath.m | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index 34a9a6e7..75f6cbd6 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -164,14 +164,14 @@ wildcardflag=0; indexstar=strfind(OutputTemplate,'*'); indexsep=strfind(OutputTemplate,filesep); %return indices of file separators -if any(indexstar>0) %% if there are any wildcards present +if(any(indexstar>0)) %% if there are any wildcards present wildcardflag=1; for index=2:length(indexsep) %run over all directory names indexsep=strfind(OutputTemplate,filesep); %update indices of separators (they might move around after substitution, but number will not change) prePath = OutputTemplate(1:(indexsep(index)-1)); postPath = OutputTemplate(indexsep(index):end); - if any(strfind(prePath,'*')>0) %if any wildcards exist in present chunk + if(any(strfind(prePath,'*')>0)) %if any wildcards exist in present chunk [preParent, preWild, preExt] = fileparts(prePath); preWild = [preWild preExt]; starmatch=dir(prePath); @@ -202,14 +202,14 @@ end % handle the last piece -if any(strfind(OutputTemplate,'*')>0) %if any wildcards STILL exist (they must be in a file spec at the end of OutputTemplate) +if(any(strfind(OutputTemplate,'*')>0)) %if any wildcards STILL exist (they must be in a file spec at the end of OutputTemplate) starmatch=dir(OutputTemplate); [preParent, preWild, preExt] = fileparts(OutputTemplate); preWild = [preWild preExt]; switch length(starmatch) case 0 %Raise error CHECKED - if(exist('suffix') + if(exist('suffix')) errormsg=sprintf(['Error -- found no files in "%s" matching your wildcard "%s". ' ... 'Note: the suffix "%s" may have been added to your wildcard. ' ... 'Please look at your use of wildcards.'], ... @@ -228,7 +228,7 @@ Match = starmatch.name ; OutputTemplate=fullfile(Parent,Match) ; otherwise - if exist('suffix') + if exist(('suffix')) errormsg = sprintf(['Error -- More than one file found in "%s" matches your wildcard "%s". ' ... 'Note: the suffix "%s" may have been added to your wildcard. Please check your use of wildcards.'], ... preParent, preWild, suffix); @@ -251,7 +251,7 @@ %% Check if path exists (if supposed to) -if strcmpi('check',mode) +if(strcmpi('check',mode)) if exist(OutputTemplate,'file') == 0 errormsg = sprintf(['Error -- it appears that the directory or file "%s" does not exist. ' ... 'Double check that you haven''t made a typo and that the file actually exists'],OutputTemplate); @@ -263,7 +263,7 @@ %% Make path if it doesn't exist (if supposed to) -if strcmpi('makedir',mode) && wildcardflag==0 +if(strcmpi('makedir',mode) && wildcardflag==0) if exist(OutputTemplate,'file') == 0 try mkdir(OutputTemplate) @@ -278,7 +278,7 @@ end %% Make parent path if it doesn't exist (if supposed to) -if strcmpi('makeparentdir',mode) && wildcardflag==0 +if(strcmpi('makeparentdir',mode) && wildcardflag==0) [templatepath, templatename, templatext, templateversn] = fileparts(OutputTemplate); if exist(templatepath,'file') == 0 try From 6124e972a6862e527b857ecb4f695f89a2225c82 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 8 Mar 2012 13:59:46 -0500 Subject: [PATCH 32/48] move bracket expansion up to happen first --- matlabScripts/mc_GenPath.m | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index 75f6cbd6..91a3b420 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -89,14 +89,6 @@ if(isfield(Template,'type')) type=Template.type; end; Template = Template.Template; end - -%% Clean up template based on type -if(type==1) - if(~strcmpi(Template(end),filesep)) - Template = [Template filesep]; - end -end - %% Do bracket expansion % Parse Template to Identify Variables index1=strfind(Template,'['); @@ -133,15 +125,7 @@ end end -%% Check if file ends with suffix. If not, append it. -if(exist('suffix') && ~strcmpi('makedir',mode)) - if(~strcmp(OutputTemplate((length(OutputTemplate)-length(suffix)+1):length(OutputTemplate)),suffix)) - OutputTemplate = [OutputTemplate suffix]; - end -end - - -%% Reconstruct the path, piece by piece, substituting in variable values +% Reconstruct the path, piece by piece, substituting in variable values OutputTemplate =[]; for k=1:length(VariableList) @@ -160,6 +144,23 @@ OutputTemplate = [OutputTemplate TemplatePart{k+1}]; +%% Check if file ends with suffix. If not, append it. +if(exist('suffix') && ~strcmpi('makedir',mode)) + if(~strcmp(OutputTemplate((length(OutputTemplate)-length(suffix)+1):length(OutputTemplate)),suffix)) + OutputTemplate = [OutputTemplate suffix]; + end +end + + +%% Clean up template based on type +if(type==1) + if(~strcmpi(Template(end),filesep)) + Template = [Template filesep]; + end +end + + + %% Handle cases with wildcards wildcardflag=0; indexstar=strfind(OutputTemplate,'*'); From dc62fdb700b5147c91cdd7106e024d529a6fa27e Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 8 Mar 2012 14:00:57 -0500 Subject: [PATCH 33/48] moved DirCheck up --- matlabScripts/mc_GenPath.m | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index 91a3b420..21f6e4c5 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -144,14 +144,6 @@ OutputTemplate = [OutputTemplate TemplatePart{k+1}]; -%% Check if file ends with suffix. If not, append it. -if(exist('suffix') && ~strcmpi('makedir',mode)) - if(~strcmp(OutputTemplate((length(OutputTemplate)-length(suffix)+1):length(OutputTemplate)),suffix)) - OutputTemplate = [OutputTemplate suffix]; - end -end - - %% Clean up template based on type if(type==1) if(~strcmpi(Template(end),filesep)) @@ -160,6 +152,14 @@ end +%% Check if file ends with suffix. If not, append it. +if(exist('suffix') && ~strcmpi('makedir',mode)) + if(~strcmp(OutputTemplate((length(OutputTemplate)-length(suffix)+1):length(OutputTemplate)),suffix)) + OutputTemplate = [OutputTemplate suffix]; + end +end + + %% Handle cases with wildcards wildcardflag=0; From 31dd321d2accea3f07500578d57ee2e939477a0a Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 8 Mar 2012 14:02:27 -0500 Subject: [PATCH 34/48] update Dircheck comment, edit it to run on OutputTemplate (result of bracket expansion), and clear suffix if dircheck is enabled --- matlabScripts/mc_GenPath.m | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index 21f6e4c5..178d54b8 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -144,11 +144,12 @@ OutputTemplate = [OutputTemplate TemplatePart{k+1}]; -%% Clean up template based on type +%% DirCheck: Clean up template based on type if(type==1) - if(~strcmpi(Template(end),filesep)) - Template = [Template filesep]; + if(~strcmpi(OutputTemplate(end),filesep)) + OutputTemplate = [OutputTemplate filesep]; end + clear('suffix') %Disable suffix mode from running if DirCheck is on end From 366cc96a2e7968d561a2fd999e7ff7c8defd5a1c Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 8 Mar 2012 14:03:48 -0500 Subject: [PATCH 35/48] update suffixcheck to be based only on existence of suffix, and to disable make mode if otherwise enabled --- matlabScripts/mc_GenPath.m | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index 178d54b8..24d09e6d 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -154,10 +154,13 @@ %% Check if file ends with suffix. If not, append it. -if(exist('suffix') && ~strcmpi('makedir',mode)) +if(exist('suffix') if(~strcmp(OutputTemplate((length(OutputTemplate)-length(suffix)+1):length(OutputTemplate)),suffix)) OutputTemplate = [OutputTemplate suffix]; end + if(strcmpi('make',mode) %If currently running in make mode, disable it + mode='null'; + end end From 7f81dfb52b14dd621b5d51a6d9c081ab9efff22a Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 8 Mar 2012 14:07:55 -0500 Subject: [PATCH 36/48] add precedence handling for wildcard depending on location of wildcard --- matlabScripts/mc_GenPath.m | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index 24d09e6d..a2fa9d89 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -166,6 +166,25 @@ %% Handle cases with wildcards + +% Precedence Handling + +[wildPath wildFile wildExt] = fileparts(OutputTemplate); +wildFile = [wildFile wildExt]; +if(any(strfind(wildFile,'*')>0)) + if(strcmpi('make',mode)) %If currently running in make mode, disable it + mode='null'; + end +end + +if(any(strfind(wildFile,'*')>0)) + if(strcmpi('makeparentdir',mode)) %If currently running in makeparentdir mode, disable it + mode='null'; + end +end + +%Do actual substitution + wildcardflag=0; indexstar=strfind(OutputTemplate,'*'); indexsep=strfind(OutputTemplate,filesep); %return indices of file separators From db048658eccb6c39f298d73cc64a86b338f91910 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 8 Mar 2012 14:11:41 -0500 Subject: [PATCH 37/48] improved readability of structure variable reading --- matlabScripts/mc_GenPath.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index a2fa9d89..a11c7edb 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -84,9 +84,9 @@ mode='null'; if(isstruct(Template)) - if(isfield(Template,'mode')) mode=Template.mode; end; - if(isfield(Template,'suffix')) suffix=Template.suffix; end; - if(isfield(Template,'type')) type=Template.type; end; + if(isfield(Template,'mode')); mode=Template.mode; end; + if(isfield(Template,'suffix')); suffix=Template.suffix; end; + if(isfield(Template,'type')); type=Template.type; end; Template = Template.Template; end %% Do bracket expansion From 7c6ffe27247bb9f844fdabcbaf80a3e346e2b04e Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 8 Mar 2012 14:12:04 -0500 Subject: [PATCH 38/48] fixed parentheses imbalance --- matlabScripts/mc_GenPath.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index a11c7edb..2b7ca9d3 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -154,11 +154,11 @@ %% Check if file ends with suffix. If not, append it. -if(exist('suffix') +if(exist('suffix')) if(~strcmp(OutputTemplate((length(OutputTemplate)-length(suffix)+1):length(OutputTemplate)),suffix)) OutputTemplate = [OutputTemplate suffix]; end - if(strcmpi('make',mode) %If currently running in make mode, disable it + if(strcmpi('make',mode)) %If currently running in make mode, disable it mode='null'; end end From 93bdf6276a17e372dc1773c3deab3e43156923da Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 8 Mar 2012 14:12:39 -0500 Subject: [PATCH 39/48] cleaned up precedence handling --- matlabScripts/mc_GenPath.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index 2b7ca9d3..ba8715ea 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -169,7 +169,7 @@ % Precedence Handling -[wildPath wildFile wildExt] = fileparts(OutputTemplate); +[wildPath, wildFile, wildExt] = fileparts(OutputTemplate); wildFile = [wildFile wildExt]; if(any(strfind(wildFile,'*')>0)) if(strcmpi('make',mode)) %If currently running in make mode, disable it @@ -183,7 +183,7 @@ end end -%Do actual substitution +% Do actual substitution wildcardflag=0; indexstar=strfind(OutputTemplate,'*'); From 6313ef25c537959269bd165503cba8dec9ae3875 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 8 Mar 2012 14:13:42 -0500 Subject: [PATCH 40/48] removed reference to wildcardflag. Precendence handling is much cleaner now and this is not necessary --- matlabScripts/mc_GenPath.m | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index ba8715ea..4d181fca 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -185,11 +185,9 @@ % Do actual substitution -wildcardflag=0; indexstar=strfind(OutputTemplate,'*'); indexsep=strfind(OutputTemplate,filesep); %return indices of file separators if(any(indexstar>0)) %% if there are any wildcards present - wildcardflag=1; for index=2:length(indexsep) %run over all directory names indexsep=strfind(OutputTemplate,filesep); %update indices of separators (they might move around after substitution, but number will not change) prePath = OutputTemplate(1:(indexsep(index)-1)); @@ -287,7 +285,7 @@ %% Make path if it doesn't exist (if supposed to) -if(strcmpi('makedir',mode) && wildcardflag==0) +if(strcmpi('makedir',mode)) if exist(OutputTemplate,'file') == 0 try mkdir(OutputTemplate) @@ -302,7 +300,7 @@ end %% Make parent path if it doesn't exist (if supposed to) -if(strcmpi('makeparentdir',mode) && wildcardflag==0) +if(strcmpi('makeparentdir',mode)) [templatepath, templatename, templatext, templateversn] = fileparts(OutputTemplate); if exist(templatepath,'file') == 0 try From 37758876e57eae8dcf1cb355f03006f292a169b0 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 8 Mar 2012 14:15:00 -0500 Subject: [PATCH 41/48] moved makedir mode up --- matlabScripts/mc_GenPath.m | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index 4d181fca..13ff1eca 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -269,21 +269,6 @@ - - - -%% Check if path exists (if supposed to) -if(strcmpi('check',mode)) - if exist(OutputTemplate,'file') == 0 - errormsg = sprintf(['Error -- it appears that the directory or file "%s" does not exist. ' ... - 'Double check that you haven''t made a typo and that the file actually exists'],OutputTemplate); - errordlg(errormsg,'Path Generation Error'); - error(errormsg) - - end -end - - %% Make path if it doesn't exist (if supposed to) if(strcmpi('makedir',mode)) if exist(OutputTemplate,'file') == 0 @@ -299,6 +284,21 @@ end end + +%% Check if path exists (if supposed to) +if(strcmpi('check',mode)) + if exist(OutputTemplate,'file') == 0 + errormsg = sprintf(['Error -- it appears that the directory or file "%s" does not exist. ' ... + 'Double check that you haven''t made a typo and that the file actually exists'],OutputTemplate); + errordlg(errormsg,'Path Generation Error'); + error(errormsg) + + end +end + + + + %% Make parent path if it doesn't exist (if supposed to) if(strcmpi('makeparentdir',mode)) [templatepath, templatename, templatext, templateversn] = fileparts(OutputTemplate); From ac25745f2ac03bbb1f93a9ceb2f268163641e6bf Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 8 Mar 2012 14:16:41 -0500 Subject: [PATCH 42/48] moved makeparentdir mode up --- matlabScripts/mc_GenPath.m | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index 13ff1eca..8e533444 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -284,21 +284,6 @@ end end - -%% Check if path exists (if supposed to) -if(strcmpi('check',mode)) - if exist(OutputTemplate,'file') == 0 - errormsg = sprintf(['Error -- it appears that the directory or file "%s" does not exist. ' ... - 'Double check that you haven''t made a typo and that the file actually exists'],OutputTemplate); - errordlg(errormsg,'Path Generation Error'); - error(errormsg) - - end -end - - - - %% Make parent path if it doesn't exist (if supposed to) if(strcmpi('makeparentdir',mode)) [templatepath, templatename, templatext, templateversn] = fileparts(OutputTemplate); @@ -317,5 +302,20 @@ +%% Check if path exists (if supposed to) +if(strcmpi('check',mode)) + if exist(OutputTemplate,'file') == 0 + errormsg = sprintf(['Error -- it appears that the directory or file "%s" does not exist. ' ... + 'Double check that you haven''t made a typo and that the file actually exists'],OutputTemplate); + errordlg(errormsg,'Path Generation Error'); + error(errormsg) + + end +end + + + + + %% End the function end From c0c8e5d39d6fbd1c798c939242e8a14877fc9af8 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 8 Mar 2012 14:17:34 -0500 Subject: [PATCH 43/48] removed some excess whitespace --- matlabScripts/mc_GenPath.m | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index 8e533444..aff63879 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -152,7 +152,6 @@ clear('suffix') %Disable suffix mode from running if DirCheck is on end - %% Check if file ends with suffix. If not, append it. if(exist('suffix')) if(~strcmp(OutputTemplate((length(OutputTemplate)-length(suffix)+1):length(OutputTemplate)),suffix)) @@ -163,8 +162,6 @@ end end - - %% Handle cases with wildcards % Precedence Handling @@ -199,7 +196,6 @@ starmatch=dir(prePath); switch length(starmatch) case 0 - %Raise error CHECKED errormsg = sprintf(['Error -- No subdirectories found in "%s" that match your wildcard expression "%s". ' ... 'Please check your use of wildcards.'], ... @@ -267,8 +263,6 @@ end end - - %% Make path if it doesn't exist (if supposed to) if(strcmpi('makedir',mode)) if exist(OutputTemplate,'file') == 0 @@ -283,7 +277,6 @@ end end end - %% Make parent path if it doesn't exist (if supposed to) if(strcmpi('makeparentdir',mode)) [templatepath, templatename, templatext, templateversn] = fileparts(OutputTemplate); @@ -299,9 +292,6 @@ end end end - - - %% Check if path exists (if supposed to) if(strcmpi('check',mode)) if exist(OutputTemplate,'file') == 0 @@ -313,9 +303,5 @@ end end - - - - %% End the function end From 6822e11f9f26149980c1b89a45989d0b1816a326 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 8 Mar 2012 14:19:55 -0500 Subject: [PATCH 44/48] fixed precedence handling for wildParent --- matlabScripts/mc_GenPath.m | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index aff63879..59b79ea3 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -168,13 +168,16 @@ [wildPath, wildFile, wildExt] = fileparts(OutputTemplate); wildFile = [wildFile wildExt]; +[null , wildParent, wildParentExt] = fileparts(wildPath); +wildParent = [wildParent wildParentExt]; + if(any(strfind(wildFile,'*')>0)) if(strcmpi('make',mode)) %If currently running in make mode, disable it mode='null'; end end -if(any(strfind(wildFile,'*')>0)) +if(any(strfind(wildParent,'*')>0)) if(strcmpi('makeparentdir',mode)) %If currently running in makeparentdir mode, disable it mode='null'; end From 42df2dfbce45ceafdfdb6ed41eabdfdce9acda56 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 8 Mar 2012 17:04:50 -0500 Subject: [PATCH 45/48] updated help file to be consistent with new option handling --- matlabScripts/mc_GenPath.m | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index 59b79ea3..bc1ddc88 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -21,13 +21,12 @@ % to extant file or directory, and raise error % message for user if not. % -% 'makedir' - Make the directory as specified by the -% path template exactly. Be careful in using this, as -% you could end up with directories named -% 'run_01.nii' if your path returns a pointer to what -% should be a file rather than a directory. If -% Template includes any wildcard this mode will be -% disabled. +% 'makedir' - After doing bracket and wildcard +% expansion, make the directory returned by the +% function. If you have specified a suffix, this mode +% will be disabled. Also, if, prior to wildcard +% expansion, the template includes a wildcard in the +% final "file" part, this mode will be disabled. % % 'makeparentdir' - Parse out the parent path by % removing the "file" part of your path (anything @@ -35,12 +34,11 @@ % "/"). This is useful if GeneratePath is returning % an absolute path to a file you're planning to make % later, but for now you want it to make a directory -% where you can place this file. If Template includes -% any wildcards this mode will be disabled. +% where you can place this file. If there is a +% wildcard in the parent directory specification, +% this mode will be disabled. % -% NOTE - If STRUCT.Template includes any wildcards, -% both makedir and makeparentdir modes will be -% disabled. +\ % % STRUCT.suffix Optional - If you expect the final resolution of the call to % include be a particular suffix (e.g. .nii) indicate @@ -58,6 +56,8 @@ % 1 - STRUCT.Template should resolve to a DIRECTORY % path. This will influence appropriateness of error % messages when failing to find a particular path. +% Also, if you specify this, it will disable suffix +% mode. % % % From 4165138d72ac6355e30d574181b69b055f00a08d Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Thu, 8 Mar 2012 17:05:44 -0500 Subject: [PATCH 46/48] reordered help file to be consistent with precedence of option handling --- matlabScripts/mc_GenPath.m | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index bc1ddc88..0b70eac6 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -14,6 +14,24 @@ % with the current value of the variable Exp, etc. % Wildcards are allowed throughout to signify none, % one, or many characters. +% STRUCT.type Numeric. Can be... +% +% 1 - STRUCT.Template should resolve to a DIRECTORY +% path. This will influence appropriateness of error +% messages when failing to find a particular path. +% Also, if you specify this, it will disable suffix +% mode. +% STRUCT.suffix Optional - If you expect the final resolution of the call to +% include be a particular suffix (e.g. .nii) indicate +% it in this slot. This will use the suffix to +% whittle down the list of potential matches when +% working with wildcards, and, if the final result +% does not end in the specified suffix, it will be +% appended. Suffix means suffix, not extension, so if +% you want to make sure a file ends in .nii, include +% the dot in STRUCT.suffix +% NOTE - Specifying a suffix will disable makedir +% mode. % % STRUCT.mode Optional - String to specify the run mode. Can be... % @@ -38,26 +56,8 @@ % wildcard in the parent directory specification, % this mode will be disabled. % -\ % -% STRUCT.suffix Optional - If you expect the final resolution of the call to -% include be a particular suffix (e.g. .nii) indicate -% it in this slot. This will use the suffix to -% whittle down the list of potential matches when -% working with wildcards, and, if the final result -% does not end in the specified suffix, it will be -% appended. Suffix means suffix, not extension, so if -% you want to make sure a file ends in .nii, include -% the dot in STRUCT.suffix -% NOTE - Specifying a suffix will disable makedir mode. -% -% STRUCT.type Numeric. Can be... % -% 1 - STRUCT.Template should resolve to a DIRECTORY -% path. This will influence appropriateness of error -% messages when failing to find a particular path. -% Also, if you specify this, it will disable suffix -% mode. % % % From 303bef3097a8d43be34b39236134f55e1d5f581e Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Tue, 13 Mar 2012 10:00:21 -0400 Subject: [PATCH 47/48] removed newfile since it was introduced by an old commit --- newfile | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 newfile diff --git a/newfile b/newfile deleted file mode 100644 index e69de29b..00000000 From e947672e8d856a448bb3657eec31a9d2d1559bb4 Mon Sep 17 00:00:00 2001 From: Daniel A Kessler Date: Tue, 13 Mar 2012 12:06:42 -0400 Subject: [PATCH 48/48] finished up testing for GenPath.m and made some fixes based on inconsistent behavior Should fix #22 --- matlabScripts/mc_GenPath.m | 184 +++++++++++++++++++++---------------- 1 file changed, 105 insertions(+), 79 deletions(-) diff --git a/matlabScripts/mc_GenPath.m b/matlabScripts/mc_GenPath.m index 0b70eac6..efe7735f 100644 --- a/matlabScripts/mc_GenPath.m +++ b/matlabScripts/mc_GenPath.m @@ -123,27 +123,31 @@ else TemplatePart{k+2}=''; end -end -% Reconstruct the path, piece by piece, substituting in variable values -OutputTemplate =[]; - -for k=1:length(VariableList) - try - VarValue = evalin('caller',VariableList{k}); - catch - errormsg = sprintf(['Error -- The variable "%s" that you enclosed in brackets does not have a ' ... - 'defined value. Double check that you have not made a typo (e.g. [EXP] instead of Exp) and carefully ' ... - 'read the commented instructions around your path template specification to be sure of which variables ' ... - 'you can use in bracketed expressions.'],VariableList{k}); - errordlg(errormsg,'Path Generation Error') - error(errormsg) + + % Reconstruct the path, piece by piece, substituting in variable values + OutputTemplate =[]; + + for k=1:length(VariableList) + try + VarValue = evalin('caller',VariableList{k}); + catch + errormsg = sprintf(['Error -- The variable "%s" that you enclosed in brackets does not have a ' ... + 'defined value. Double check that you have not made a typo (e.g. [EXP] instead of Exp) and carefully ' ... + 'read the commented instructions around your path template specification to be sure of which variables ' ... + 'you can use in bracketed expressions.'],VariableList{k}); + errordlg(errormsg,'Path Generation Error') + error(errormsg) + end + OutputTemplate=horzcat(OutputTemplate,TemplatePart{k},VarValue); %This appears to reconstruct the template without the brackets around the variables end - OutputTemplate=horzcat(OutputTemplate,TemplatePart{k},VarValue); %This appears to reconstruct the template without the brackets around the variables -end -OutputTemplate = [OutputTemplate TemplatePart{k+1}]; + OutputTemplate = [OutputTemplate TemplatePart{k+1}]; + +else + OutputTemplate = Template; +end %% DirCheck: Clean up template based on type if(type==1) if(~strcmpi(OutputTemplate(end),filesep)) @@ -196,85 +200,107 @@ if(any(strfind(prePath,'*')>0)) %if any wildcards exist in present chunk [preParent, preWild, preExt] = fileparts(prePath); preWild = [preWild preExt]; - starmatch=dir(prePath); - switch length(starmatch) - case 0 - %Raise error CHECKED - errormsg = sprintf(['Error -- No subdirectories found in "%s" that match your wildcard expression "%s". ' ... + + if (~isdir(preParent)) + errormsg = sprintf(['Error -- I was trying to find wildcard matches for "%s" in "%s" but it turns out "%s" doesn''t even ' ... + 'exist, so that''s not going to work out. Please check your path specification up to the present wildcard.'], ... + preWild, preParent, preParent); + errordlg(errormsg,'Path Generation Error') + error(errormsg) + else + starmatch=dir(prePath); + starmatch=starmatch([starmatch.isdir]); %Return only the elements that are dir + + switch length(starmatch) + case 0 + %Raise error CHECKED + errormsg = sprintf(['Error -- No subdirectories found in "%s" that match your wildcard expression "%s". ' ... + 'Please check your use of wildcards.'], ... + preParent, preWild); + errordlg(errormsg,'Path Generation Error') + error(errormsg) + case 1 + preParent = fileparts (prePath) ; + preMatch = starmatch.name ; + prePath=fullfile(preParent,preMatch) ; + OutputTemplate = [prePath postPath] ; + otherwise + %CHECKED + errormsg = sprintf(['Error -- More than one subdirectory found in "%s" matches your wildcard expression "%s". ' ... + 'Please check your use of wildcards.'], ... + preParent, preWild); + errordlg(errormsg,'Path Generation Error') + error(errormsg) + end + end + end + end +end + +% handle the last piece +if(any(strfind(OutputTemplate,'*')>0)) %if any wildcards STILL exist (they must be in a file spec at the end of OutputTemplate) + [preParent, preWild, preExt] = fileparts(OutputTemplate); + if (~isdir(preParent)) + errormsg = sprintf(['Error -- I was trying to find wildcard matches for "%s" in "%s" but it turns out "%s" doesn''t even ' ... + 'exist, so that''s not going to work out. Please check your path specification up to the present wildcard.'], ... + preWild, preParent, preParent); + errordlg(errormsg,'Path Generation Error') + error(errormsg) + else + starmatch=dir(OutputTemplate); + starmatch=starmatch(~[starmatch.isdir]); %Return only the nondir elements + + [preParent, preWild, preExt] = fileparts(OutputTemplate); + preWild = [preWild preExt]; + switch length(starmatch) + case 0 + %Raise error CHECKED + if(exist('suffix')) + errormsg=sprintf(['Error -- found no files in "%s" matching your wildcard "%s". ' ... + 'Note: the suffix "%s" may have been added to your wildcard. ' ... + 'Please look at your use of wildcards.'], ... + preParent,preWild,suffix); + errordlg(errormsg,'Path Generation Error') + error(errormsg) + else + errormsg = sprintf(['Error -- No files found in "%s" that match your wildcard expression "%s". ' ... 'Please check your use of wildcards.'], ... preParent, preWild); errordlg(errormsg,'Path Generation Error') error(errormsg) - case 1 - preParent = fileparts (prePath) ; - preMatch = starmatch.name ; - prePath=fullfile(preParent,preMatch) ; - OutputTemplate = [prePath postPath] ; - otherwise - %CHECKED - errormsg = sprintf(['Error -- More than one subdirectory found in "%s" matches your wildcard expression "%s". ' ... + end + case 1 + Parent = fileparts (OutputTemplate) ; + Match = starmatch.name ; + OutputTemplate=fullfile(Parent,Match) ; + otherwise + if exist(('suffix')) + errormsg = sprintf(['Error -- More than one file found in "%s" matches your wildcard "%s". ' ... + 'Note: the suffix "%s" may have been added to your wildcard. Please check your use of wildcards.'], ... + preParent, preWild, suffix); + errordlg(errormsg,'Path Generation Error') + error(errormsg) + else + %Checked + errormsg = sprintf(['More than one file found in "%s" matches your wildcard expression "%s". ' ... 'Please check your use of wildcards.'], ... preParent, preWild); errordlg(errormsg,'Path Generation Error') error(errormsg) - end + end end end end -% handle the last piece -if(any(strfind(OutputTemplate,'*')>0)) %if any wildcards STILL exist (they must be in a file spec at the end of OutputTemplate) - starmatch=dir(OutputTemplate); - [preParent, preWild, preExt] = fileparts(OutputTemplate); - preWild = [preWild preExt]; - switch length(starmatch) - case 0 - %Raise error CHECKED - if(exist('suffix')) - errormsg=sprintf(['Error -- found no files in "%s" matching your wildcard "%s". ' ... - 'Note: the suffix "%s" may have been added to your wildcard. ' ... - 'Please look at your use of wildcards.'], ... - preParent,preWild,suffix); - errordlg(errormsg,'Path Generation Error') - error(errormsg) - else - errormsg = sprintf(['Error -- No files found in "%s" that match your wildcard expression "%s". ' ... - 'Please check your use of wildcards.'], ... - preParent, preWild); - errordlg(errormsg,'Path Generation Error') - error(errormsg) - end - case 1 - Parent = fileparts (OutputTemplate) ; - Match = starmatch.name ; - OutputTemplate=fullfile(Parent,Match) ; - otherwise - if exist(('suffix')) - errormsg = sprintf(['Error -- More than one file found in "%s" matches your wildcard "%s". ' ... - 'Note: the suffix "%s" may have been added to your wildcard. Please check your use of wildcards.'], ... - preParent, preWild, suffix); - errordlg(errormsg,'Path Generation Error') - error(errormsg) - else - %Checked - errormsg = sprintf(['More than one file found in "%s" matches your wildcard expression "%s". ' ... - 'Please check your use of wildcards.'], ... - preParent, preWild); - errordlg(errormsg,'Path Generation Error') - error(errormsg) - end - end -end - %% Make path if it doesn't exist (if supposed to) if(strcmpi('makedir',mode)) if exist(OutputTemplate,'file') == 0 try - mkdir(OutputTemplate) + mkdir(OutputTemplate); catch errormsg=sprintf(['Error -- there was a problem writing the file/directory "%s", perhaps you don''t ' ... 'have write permissions to the directory that you specified. Confirm that you are ' ... - 'able to make the directory manually.'],templatepath); + 'able to make the directory manually.'],OutputTemplate); errordlg(errormsg,'Path Generation Error'); error(errormsg); end @@ -285,9 +311,9 @@ [templatepath, templatename, templatext, templateversn] = fileparts(OutputTemplate); if exist(templatepath,'file') == 0 try - mkdir(templatepath) + mkdir(templatepath); catch - errordmsg=sprintf(['Error -- there was a problem making the directory "%s", perhaps you don''t ' ... + errormsg=sprintf(['Error -- there was a problem making the directory "%s", perhaps you don''t ' ... 'have write permissions to the directory that you specified. Confirm that you are ' ... 'able to make the directory manually.'],templatepath); errordlg(errormsg,'Path Generation Error');