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

crapdemo.m
%CRAPDEMO	demo script for CRAP
%SYNTAX
%		crapdemo
%INPUT
%		none
%OUTPUT
%		run-time variables are left in the workspace
%EXAMPLE
%		crapdemo

% created:
%	us	20-Sep-2007
% modified:
%	us	24-Sep-2007 01:18:41

%-------------------------------------------------------------------------------
% contents of craphtml.m
%-------------------------------------------------------------------------------
%% 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)
%% run all examples together
%
	[ap,ar]=crap(ep,er);
%%
%

% - show result
	disp(ap)
%%
	r=[ar{:}].';
	r=[ar,rtype(r+1),ap].';
	disp(sprintf('%1d/%-6s:   %s\n',r{:}));

	cd(od);
%% show various syntax possibilities
	[ap,ar]=crap('bin',matlabroot);
	disp(sprintf('%1d %s',ar,ap));
%%
	[ap,ar]=crap({'bin','bin'},matlabroot);
	disp([ar,ap]);
%%
	[ap,ar]=crap({'bin','bin'},{matlabroot});
	disp([ar,ap]);
%%
	[ap,ar]=crap({'bin','bin'},{matlabroot,matlabroot});
	disp([ar,ap]);
%%

% this will cause an error
	[ap,ar]=crap({'bin','bin'},{matlabroot,matlabroot,matlabroot});
	disp([ar,ap]);

Contact us at files@mathworks.com