image thumbnail
from crap: a pedestrian absolute folder/file path creator by us
crap creates absolute paths to folders and/or files

craphtml

Contents

CRAP description

% CRAP creates clean absolute paths for existing
%	- absolute paths to folder and/or files
%	- relative paths to folder 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);
% input:
%	F|F	a character string or a cell of character strings
%	R	a character string or a cell of character strings
% 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

set paths/files

% - root folder
	rd=sprintf('%s/toolbox/matlab/datafun',matlabroot);
% - paths
	ep={
		rd
		[rd,'/../']
		'../datatypes/@char'
		'../../matlab/datatypes/../datatypes/@char'
		'../../matlab/datafun/../../matlab/elmat/../elmat/magic.m'
		'./'
		'./../elmat/magic.m'
		which('unique')
		'./../elmat/magic.FOO'		% this will cause an ERROR
		'bin'				% use another root folder
	};
% - root folders (empty: use current folder by default)
	er=[
		repmat({[]},numel(ep)-1,1)
		{matlabroot}			% use another root folder
	];
% - define some parameters
	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};
% - run crap
	[ap,ar]=crap(ce,cr);
% - show output
	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

% NOTE
% - all file separators are slashes, which work on windows as well!
% - folders are marked with a trailing slash
% - if root entries are empty, the current root was used (default)
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

% - show result
	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/'

% this will cause an error
	[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})

Contact us at files@mathworks.com