Info

This question is closed. Reopen it to edit or answer.

How do I refer to a previously defined argumentlist of symbolic variables while creating a Anonymous Function?

1 view (last 30 days)
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 Comments
Oliver Brieger
Oliver Brieger on 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.

Answers (1)

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