@us
Thank you very much for you comments. I learned a lot about mlint and regexp from your simple codes. I really wanted this kind of productive communication. Let me upload my 'hopefully' final version soon, which is also inspired by you too. :)
you could use this engine to dynamically assign function handles to all 1.st level subfunctions in a file, which may return a variable number of output args
function fh=foo
x=mlint(mfilename,'-calls');
r=regexp({x.message}.','(?<=(S\d\s+\d+\s+\d+\s+))\w+','match');
r=[r{:}].';
for i=1:numel(r)
fh.(r{i})=str2func(r{i});
end
end
% subroutines
function varargout=msub(a,b)
r={
a-b
a
b
nan
};
varargout(1:nargout)={[]};
narg=min([nargout,size(r,1)]);
[varargout{1:narg}]=deal(r{1:narg});
end
function r=madd(a,b)
r=a+b;
end
function r=mmul(a,b)
r=a.*b;
end
% in the command window
fh=foo;
[a,b]=fh.msub(1,2)
% a=-1,b=1
[a,b,c,d,e]=fh.msub(1,2)
% a=-1,b=1,c=2,d=nan,e=[]
us
@us
I tried your solution and found one negligible drawback, which is that you have to add each function's name in the top function and you have to make a handle in the command line whenever you want to use the function in a different namespace. This is still true when I use a class. But still I think your solution is more elegant because it does not use 'eval' which I don't like either.
@us
Thank you for your fast reply. Your solution looks much better. I really appreciate it. I'll use it right away. Thanks for your time for reviewing my file.
Comment only