0001 function [FinalDir, ErrorFlag] = gotodirectory(GotoDir)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 ErrorFlag = 0;
0012
0013 if nargin == 0
0014 FinalDir = [pwd filesep];
0015 return
0016 elseif isempty(GotoDir)
0017 FinalDir = [pwd filesep];
0018 return
0019 elseif strcmp(GotoDir,'.')
0020 FinalDir = [pwd filesep];
0021 return
0022 end
0023
0024
0025
0026
0027
0028
0029
0030 k = findstr(GotoDir, filesep);
0031
0032 if isempty(k)
0033
0034 GotoDir(end+1) = filesep;
0035 k(1) = length(GotoDir);
0036 else
0037
0038 if k(1) == 1
0039 k(1) = [];
0040 end
0041
0042 if length(k) >= 1
0043 if k(1) == 2
0044 k(1) = [];
0045 if ispc && length(k) >= 1
0046 k(1) = [];
0047 end
0048 end
0049 end
0050
0051 if length(k) >= 1
0052 if k(1) == 3
0053 k(1) = [];
0054 end
0055 end
0056 end
0057
0058
0059 if ~strcmp(GotoDir(end), filesep)
0060 GotoDir(end+1) = filesep;
0061 k(end+1) = length(GotoDir);
0062 end
0063
0064
0065
0066 j = 1;
0067 for i = 1:length(k)
0068
0069 try
0070 cd(GotoDir(j:k(i)));
0071 catch
0072
0073 [Success, msg] = mkdir(GotoDir(j:k(i)));
0074 if Success
0075 cd(GotoDir(j:k(i)));
0076 else
0077 if ispc
0078 ErrorFlag = 1;
0079 fprintf(' Problem creating directory: %s\n', msg);
0080 break
0081 else
0082
0083 try
0084 cd(GotoDir(j:k(i)));
0085 catch
0086 ErrorFlag = 1;
0087 fprintf(' Problem creating directory: %s\n', msg);
0088 break
0089 end
0090 end
0091 end
0092 end
0093 j = k(i) + 1;
0094 end
0095
0096
0097
0098 FinalDir = pwd;
0099 FinalDir = [FinalDir filesep];
0100