How can I use the number of variables as expression in a logical if Statement?
Show older comments
I have a function that operates with two function handles as input arguments and creates the hessian matrix of another function later. I want to calculate the eigenvalues of that hessian matrix at certain points, so that I converted the matrix into a function handle using 'matlabFunction'.
The problem I got now is that I don't know, whether the hessian matrix is a function in x and y, in only one or in none variables, so that the input hessian(x,y) is not defined if there are less then two variables. My idea is to seperate the cases in an if-statement so that I can have only one input, but I don't know what kind of expression would fit for the condition and how to seperate the cases when there is one variable in the matrix, like P=[1;0] hessian=[1 0; 0 y].
9 Comments
Walter Roberson
on 18 Feb 2021
nargin(FunctionHandle)
perhaps ?
Arne Kähler
on 18 Feb 2021
Fangjun Jiang
on 18 Feb 2021
check out inputname()
Walter Roberson
on 18 Feb 2021
inputname() call tell you the name of the variable the user passed as a parmeter, if the user used a variable at all and not an expression. However, if you are planning to look at whether the user used variable name x or y then that is not good programming practice -- suppose the user used Y or t or something else of meaning to them ?
Arne Kähler
on 18 Feb 2021
Fangjun Jiang
on 18 Feb 2021
inputname() returns 'x' or 'y', use strcmp()
Jan
on 18 Feb 2021
@Arne Kähler: Please post a minimal working example.
Arne Kähler
on 18 Feb 2021
Fangjun Jiang
on 18 Feb 2021
doc inputname
Answers (0)
Categories
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!