MATLAB: How to ask the user to input a function only once

functionshandlesinput

Hello there, so basically what i am trying to do is to make a program that asks the user for a function ('for example : f = @(x) input('Please enter your function :'); and some other variables to solve it, but the only problem i'm facing is when i affect my function to a variable (f in this case) it keeps asking me to enter the function each time i use this variable in the code, it works in the end when i write down the function four times but it's a bit ugly…
So what i want is to ask the user for a function with the input command and to work with that function to the end of the program even when it face the same variable (f) that asks for input, or maybe another alternative to solve this and thanks in advance.

Best Answer

funstr = input('Please enter your function: ', 's');
f = str2func( ['@(x) ' funstr ] );
Related Question