How to display a graph after getting user to input in the pop up?
1 view (last 30 days)
Show older comments
Question:
A window should pop up that asks the user to input a single variable equation (a.k.a. function) for which the user wishes to find the root.
ii) A graph of this equation should get displayed
My code:
prompt = {'Single variable equation: \color{white} .'};
dlg_title = 'Input';
num_lines = 1;
defaultans = {'x'};
options.Interpreter='tex';
answer = inputdlg(prompt,dlg_title,num_lines,defaultans,options);
func = string(answer{1});
What to do next? New to MATLAB, please help.
Accepted Answer
Walter Roberson
on 9 Sep 2017
6 Comments
Walter Roberson
on 12 Sep 2017
How do I know that you are not going to just delete your question after I answer it? When you delete your question after I have answered, you treat me as if I am a free private consultant. If you want a private consultation then you can hire a private consultant; if you want free advice then the price we charge is that your question and the responses must remain public for everyone to learn from.
Walter Roberson
on 12 Sep 2017
The meaning of
@(x) func
is the same as
@(x) func()
which means that a local variable named "x" is to be accepted on input, and then func() is to be called with no parameters.
The syntax
@(x) func(x)
would mean that a local variable named "x" is to be accepted on input, and then func() is to be called passing that variable as a parameter. @(x) func(x) means almost exactly the same thing as @func alone without the (x) part, except that using @(x) func(x) will only work if exactly one input is provided, whereas @func would work if multiple inputs are provided if func accepts multiple inputs.
In
f = str2func((['@(',char(symvar(func)),')', func]));
then notice the symvar(func) and at that point func is a character vector. symvar(func) looks through the string to figure out which variable the user used -- so for example if the user had entered 'sin(t).^2' then symvar would return the 't' . When symvar is used properly, the user is not restricted to using a particular variable name.
The above code constructs @(VARIABLENAME) CODE . In the 'sin(t).^2' example, @(t) sin(t).^2 would be constructed; if the user had coded 'besselj(1/2, x)' then @(x) besselj(1/2,x) would be constructed.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!