Contents
CRAP description
set paths/files
rd=sprintf('%s/toolbox/matlab/datafun',matlabroot);
ep={
rd
[rd,'/../']
'../datatypes/@char'
'../../matlab/datatypes/../datatypes/@char'
'../../matlab/datafun/../../matlab/elmat/../elmat/magic.m'
'./'
'./../elmat/magic.m'
which('unique')
'./../elmat/magic.FOO'
'bin'
};
er=[
repmat({[]},numel(ep)-1,1)
{matlabroot}
];
rtype={
'error'
'folder'
'file'
};
nr=numel(rd);
ne=numel(ep);
run examples in a loop
od=cd(rd);
for nx=1:ne
ce=ep{nx};
cr=er{nx};
[ap,ar]=crap(ce,cr);
txt={
sprintf('EXAMPLE %1d/%1d',nx,ne)
repmat('-',1,8+max([numel(ce),numel(ap),nr]))
sprintf('current %s',rd)
sprintf('root %s',cr)
sprintf('input %s',ce)
sprintf('output %s',ap)
sprintf('type %s',rtype{ar+1})
sprintf('\n')
};
disp(char(txt));
end
EXAMPLE 1/10
--------------------------------------------
current F:\usr\r2007/toolbox/matlab/datafun
root
input F:\usr\r2007/toolbox/matlab/datafun
output F:/usr/r2007/toolbox/matlab/datafun/
type folder
EXAMPLE 2/10
-----------------------------------------------
current F:\usr\r2007/toolbox/matlab/datafun
root
input F:\usr\r2007/toolbox/matlab/datafun/../
output F:/usr/r2007/toolbox/matlab/
type folder
EXAMPLE 3/10
----------------------------------------------------
current F:\usr\r2007/toolbox/matlab/datafun
root
input ../datatypes/@char
output F:/usr/r2007/toolbox/matlab/datatypes/@char/
type folder
EXAMPLE 4/10
----------------------------------------------------
current F:\usr\r2007/toolbox/matlab/datafun
root
input ../../matlab/datatypes/../datatypes/@char
output F:/usr/r2007/toolbox/matlab/datatypes/@char/
type folder
EXAMPLE 5/10
----------------------------------------------------------------
current F:\usr\r2007/toolbox/matlab/datafun
root
input ../../matlab/datafun/../../matlab/elmat/../elmat/magic.m
output F:/usr/r2007/toolbox/matlab/elmat/magic.m
type file
EXAMPLE 6/10
--------------------------------------------
current F:\usr\r2007/toolbox/matlab/datafun
root
input ./
output F:/usr/r2007/toolbox/matlab/datafun/
type folder
EXAMPLE 7/10
-------------------------------------------------
current F:\usr\r2007/toolbox/matlab/datafun
root
input ./../elmat/magic.m
output F:/usr/r2007/toolbox/matlab/elmat/magic.m
type file
EXAMPLE 8/10
------------------------------------------------
current F:\usr\r2007/toolbox/matlab/datafun
root
input F:\usr\r2007\toolbox\matlab\ops\unique.m
output F:/usr/r2007/toolbox/matlab/ops/unique.m
type file
CRAP> path/file does not exist:
F:/usr/r2007/toolbox/matlab/elmat/magic.FOO
EXAMPLE 9/10
-------------------------------------------
current F:\usr\r2007/toolbox/matlab/datafun
root
input ./../elmat/magic.FOO
output
type error
EXAMPLE 10/10
-------------------------------------------
current F:\usr\r2007/toolbox/matlab/datafun
root F:\usr\r2007
input bin
output F:/usr/r2007/bin/
type folder
run all examples together
[ap,ar]=crap(ep,er);
CRAP> path/file does not exist:
F:/usr/r2007/toolbox/matlab/elmat/magic.FOO
disp(ap)
'F:/usr/r2007/toolbox/matlab/datafun/'
'F:/usr/r2007/toolbox/matlab/'
'F:/usr/r2007/toolbox/matlab/datatypes/@char/'
'F:/usr/r2007/toolbox/matlab/datatypes/@char/'
'F:/usr/r2007/toolbox/matlab/elmat/magic.m'
'F:/usr/r2007/toolbox/matlab/datafun/'
'F:/usr/r2007/toolbox/matlab/elmat/magic.m'
'F:/usr/r2007/toolbox/matlab/ops/unique.m'
''
'F:/usr/r2007/bin/'
r=[ar{:}].';
r=[ar,rtype(r+1),ap].';
disp(sprintf('%1d/%-6s: %s\n',r{:}));
cd(od);
1/folder: F:/usr/r2007/toolbox/matlab/datafun/
1/folder: F:/usr/r2007/toolbox/matlab/
1/folder: F:/usr/r2007/toolbox/matlab/datatypes/@char/
1/folder: F:/usr/r2007/toolbox/matlab/datatypes/@char/
2/file : F:/usr/r2007/toolbox/matlab/elmat/magic.m
1/folder: F:/usr/r2007/toolbox/matlab/datafun/
2/file : F:/usr/r2007/toolbox/matlab/elmat/magic.m
2/file : F:/usr/r2007/toolbox/matlab/ops/unique.m
0/error :
1/folder: F:/usr/r2007/bin/
show various syntax possibilities
[ap,ar]=crap('bin',matlabroot);
disp(sprintf('%1d %s',ar,ap));
1 F:/usr/r2007/bin/
[ap,ar]=crap({'bin','bin'},matlabroot);
disp([ar,ap]);
[1] 'F:/usr/r2007/bin/'
[1] 'F:/usr/r2007/bin/'
[ap,ar]=crap({'bin','bin'},{matlabroot});
disp([ar,ap]);
[1] 'F:/usr/r2007/bin/'
[1] 'F:/usr/r2007/bin/'
[ap,ar]=crap({'bin','bin'},{matlabroot,matlabroot});
disp([ar,ap]);
[1] 'F:/usr/r2007/bin/'
[1] 'F:/usr/r2007/bin/'
[ap,ar]=crap({'bin','bin'},{matlabroot,matlabroot,matlabroot});
disp([ar,ap]);
CRAP> input arguments do not match!
# roots must be # paths or 1
# paths 2
# roots 3
CRAP help
help crap;
CRAP CReate Absolute Path to a folder or file
CRAP creates clean absolute paths for existing
- absolute paths to folders and/or files
- relative paths to folders and/or files
which may include any valid combination of './' or '../' syntax
absolute paths are created based
- on the current folder (default root)
- an optional root folder given as the second argument
CRAP returns the absolute path and an optional argument,
which shows the result of the operation
SYNTAX
[AP,R] = CRAP(FOLDER|FILE)
[AP,R] = CRAP(FOLDER|FILE,ROOT)
[AP,R] = CRAP({'F1','F2',...,'Fx'},ROOT)
[AP,R] = CRAP({'F1','F2'...'Fx'},{'R1','R2'...'Rx'})
[AP,R] = CRAP(N,...)
INPUT
F|F a character string or a cell of character strings
R a character string or a cell of character strings
N if the first arg is numeric, runtime messages are not shown
OUTPUT
AP absolute path of the FOLDER|FILE
R result
0 AP is valid but does NOT exist
1 AP is a folder
2 AP is a file
NOTES
- if cells are used, the number of Rs
must match the number of Fs|Fs or be 1 (one)
- if F|F is a cell and only one R is used, it
may be a character string
- if the input are cells, the output are also
cells
- leading \\ are preserved in UNC paths
EXAMPLE
rd=sprintf('%s/toolbox/matlab/datafun',matlabroot);
od=cd(rd);
ep={
[rd,'/../']
'../datatypes/@char'
'../../matlab/datatypes/../datatypes/@char'
'../../matlab/datafun/../../matlab/elmat/../elmat/magic.m'
};
for i=1:numel(ep)
ap=crap(ep{i});
disp(ap)
end
[ap,ar]=crap(ep);
disp(ap);
cd(od);
[ap,ar]=crap({'bin','bin'},matlabroot)
[ap,ar]=crap({'bin','bin'},{matlabroot,matlabroot})