Using string as input arguments of a function

8 views (last 30 days)
Let's say i have variable 'a' and this variable can have the following dependencies: @(x), @(x,y), @(x,z) and @(x,y,z).
In order to substitute the values correctly, I must write the right arguments of a, as shown below.
if isequal( symvar(a), x ) == 1
b = a(x)
elseif isequal( symvar(a), [x,y] ) == 1
b = a(x,y)
elseif isequal( symvar(a), [x,z] ) == 1
b = a(x,z)
elseif isequal( symvar(a), [x,y,z] ) == 1
b = a(x,y,z)
end
But in reality the problem I'm facing contains many variables with many dependencies like these, so it is not optimal to write if statements for them.
I was wondering if there is any way that I could use 'symvar' to verify what is the dependency, and then "copy" the answer of 'symvar' as a string into the arguments of 'a'
Example:
a = x + y;
dep_a = symvar(a)
dep_a =
[x,y]
% Now, the answer being '[x,y]' is there any way I could transform the 'x,y' in a string or something alike
% to then use it as the arguments of 'a' ?
b = a("function that writes strings as arguments of a function")
% resulting in
b = a(x,y)
  1 Comment
madhan ravi
madhan ravi on 27 Feb 2019
doc str2func
I had read the description of this function, but it is not a function handle I'm trying to create.
Take it from here:
% variable 'a' is part of a longer equation which
% has already a function handle in it
b = @(x,y,z) a(x,y)
% What I imagined was to save the 'symvar' answer
% and to write it as a string in the arguments, so when
% the code reads, it is like I wrote manually.
However, if you can show me that 'str2func' works, I would be very grateful !

Sign in to comment.

Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!