Using string as input arguments of a function

8 次查看(过去 30 天)
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 个评论
madhan ravi
madhan ravi 2019-2-27
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 !

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by