Info
Cette question est clôturée. Rouvrir pour modifier ou répondre.
How do I refer to a previously defined argumentlist of symbolic variables while creating a Anonymous Function?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello everybody,
i was wondering if there is a way to rewrite this :
a1=5;
b1=5;
t=1;
s=2;
x=1;
a=@(t,s,x) t^2*a1^2*s;
b=@(t,s,x) a(t,s,x)^0.5*b1+s;
fplot(@(t) b(t,1,2),[1 5],'b-');
To something like this :
a1=5;
b1=5;
t=1;
s=2;
x=1;
arglist=something(t,s,x);
a=@(arglist) t^2*a1^2*s;
b=@(arglist) a(arglist)^0.5*b1+s;
fplot(@(t) b(arglist),[1 5],'b-');
This is a simplification of what i want to do. In reality i have a arglist of about 30 variables, which makes it very dreadful to always copy and paste it everywhere but more importantly it's just confusing for somebody else to overread it later on... I want to be able to fplot my function in dependence of any of these 30 variables by just changing the argument in the fplot.
i understand that the functionhandle will look for only one argument named "arglist" in the second case instead of understanding that I want arglist to be a placeholder for a comma seperated list of arguments.
So is there a way to to define a list of arguments i can put into the @() ?
2 commentaires
Stephen23
le 8 Jan 2020
Modifié(e) : Stephen23
le 8 Jan 2020
(possibly using the varargin keyword), but for 30 or so parameters I would recommend avoiding using so many positional input arguments (which are very verbose and difficult to debug), and instead use one scalar structure to store those parameter values. That would mean rewriting your code slightly to refer to the fields of that structure, but after that your code would be more robust and easier to debug.
Réponses (1)
Sylvain Lacaze
le 8 Jan 2020
Hi Oliver,
I might be off, but are you looking for something like this?
f = @(varargin) cat( 2, varargin{:} )
f(1,2,3,4)
Which gives:
ans =
1 2 3 4
Apologies if I've missed what you're after.
HTH,
Sylvain
0 commentaires
Cette question est clôturée.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!