[Matlab] Function Handle-user input

  • Context: MATLAB 
  • Thread starter Thread starter Bostonpancake0
  • Start date Start date
  • Tags Tags
    Function Input Matlab
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 3K views
Bostonpancake0
Messages
42
Reaction score
0
Hi,

My question is in relation to Matlab.

I'm curious, is there anyway to employ user inputs for function handles?

By this I mean that the user is prompted to enter they're chosen function and the next line of code employs that into a handle.

My quick and rough attempt was:

a=input('enter a function in terms of x: ')
f=@(x) a

But I receive an error saying "Undefined function or variable x"

Any suggestions greatly appreciated.
 
Physics news on Phys.org
I don't think that's possible. They would have to input the entire function handle, not just a piece.

Otherwise, what is a? If they input a number, or a matrix, then "@(x) a" just returns a, and isn't really a function. If they input a string like 'sin', then "@(x) a" just doesn't make any sense. They need to input a function handle, since it is a fundamental type.

It would be similar to asking, "Input a matrix that is half int32 and half double." Not possible. Matrices can be entirely int32 or entirely double, but not both.