Using string as input arguments of a function

조회 수: 8 (최근 30일)
Felipe Businaro
Felipe Businaro 2019년 2월 26일
댓글: madhan ravi 2019년 2월 27일
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