Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Matlab] Function Handle-user input

  1. Jul 31, 2014 #1

    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.
  2. jcsd
  3. Jul 31, 2014 #2


    User Avatar
    Gold Member

    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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook