checking if a variable contains another variable
3 次查看(过去 30 天)
显示 更早的评论
hi,
i was writing some code to solve first order differential equations using euler's approximation method(no problem if you don't know what that means). i came across a situation where i want to check whether a function handle contains a specific variable or not. here is what i mean,
dydx is a function handle that is passed on as an argument into my user-defined function. dydx can be function of x alone or y alone or both. i want to evaluate dydx at some x array or y array or x , y arrays depending on what variable they contain.
of course, i could create three different user defined function. but, i want to know if there is a better way.
is there any way to check if a function handle contain this or that variable???
%if dydx is a function of x alone
m = dydx(x) %x is some 1d array
%if dydx is a function of y alone
m = dydx(y)
%if dydx is a function of both x and y
m = dydx(x , y)
this is what i want...
thanks for your help!
0 个评论
采纳的回答
更多回答(2 个)
Image Analyst
2020-11-29
OK, here's a fairly robust chunk of code that you can adapt as needed:
%-----------------------------------------------------------------------------------
% Test script:
x = 5;
y = 5;
a = 1;
dxdy(x);
dxdy(y);
dxdy(x,y);
dxdy(a,y);
%-----------------------------------------------------------------------------------
function dxdy(varargin)
numArguments = nargin; % Give it a more meaningful name.
fprintf('--------------------------------\nnargin = %d\n', numArguments);
arg1Name = inputname(1)
fprintf('The first argument is "%s".\n', arg1Name);
if numArguments >= 2
arg2Name = inputname(2)
fprintf('The second argument is "%s".\n', arg2Name);
else
fprintf('No second argument was passed in.\n');
end
% Do different things depending on what they passed in:
if numArguments >= 2
if contains(arg1Name, 'x', 'IgnoreCase', true) && contains(arg2Name, 'y', 'IgnoreCase', true)
% They passed in a variable in the first position that has x in the name
% and a variable in the second position that has y in the name.
fprintf('You passed in both x and y, so I am processing both of them.\n');
else
% They passed them in in the wrong order, like dxdy(y, x) or
% with the wrong names like dxdy(a, b).
errorMessage = sprintf('Error You passed in variables without x and y in the names.\n');
fprintf('%s\n', errorMessage);
uiwait(errordlg(errorMessage));
end
else
% They passed in just one, either x or y. Let's figure out what it was.
if contains(arg1Name, 'x', 'IgnoreCase', true)
% They passed in a variable in the first position that has x in the name.
fprintf('You passed in only x so I am processing only x.\n');
elseif contains(arg1Name, 'y', 'IgnoreCase', true)
% They passed in a variable in the first position that has y in the name.
fprintf('You passed in only y so I am processing only x.\n');
end
end
end
0 个评论
Ameer Hamza
2020-11-29
The variable name used as input to the function is arbitrary. There is no good way to distinguish between m = dydx(x) and m = dydx(y). 'x' and 'y' are just arbitrary letters. As far as MATLAB is concerned, you can use any other name. You may pass an additional input to your euler's solver function to know which type of function handle is passed
function y = myEuler(dydx, flag)
switch flag
case 1
% write code when function handle of x is passed
case 2
% write code when function handle of y is passed
case 3
% write code when function handle of x and y is passed
otherwise
% maybe throw an error and ask to pass the correct flag
end
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!