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)
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
Stephen23 le 8 Jan 2020
Modifié(e) : Stephen23 le 8 Jan 2020
You could use a comma-separated list (as Sylvain Lacaze's answer shows):
(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.
Oliver Brieger
Oliver Brieger le 8 Jan 2020
Hello Stephen!
The answers from you and Sylvain are totally what i was looking for except that i can not seem to do the fplot like i want to. I'd like the following 2 codes to do the exact same thing :
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,s,x),[1 5],'b-');
bef.png
and
a1=5;
b1=5;
t=1;
s=2;
x=1;
arglist={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-');
aft.png
As you can see in the plots, the latter code can not use the t in the comma seperated list in arglist as a variable input argument anymore. (If i understand it right). If t is a variable it is just a normal linear function as you can see in the first plot. I am sure you get my problem.
A little background info:
You are exactly right that this will become very verbose and difficult to debug. This is why i just want declare a comma seperated list only once in the beginning with ALL the variables i will use through my entire skript. Afterwards i would give every function the whole set of input arguments and would be able to fplot my solution in dependence of the one input argument i choose. I could imagine that this is just not possible for matlab, or the work around is quite complicated.. I do not have a programming background so if i want to do something impossible or stupid let me know.

Réponses (1)

Sylvain Lacaze
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

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by