Help plotting user input functions for Matlab App.
3 views (last 30 days)
Show older comments
Hi all,
I have a project where I am trying to build an app which can plot simple user functions (such as quadratic, trignometric etc). I have an edit field text where the users will be able to add thier functions in. I want it so users do not have to include 'y =' into their function, but can rather type the components with x in it. eg. they could type 2x and that would be 'y=2x' or '5x^2 + 3' would be 'y=5x^2 + 3'. I have used the callback function and included the code within:
function EnterFunctionEditFieldValueChanged(app, event)
value = app.EnterFunctionEditField.Value;
x = -10:0.1:10;
y = value;
plot (app.UIAxes, x, y);
end
Now I am aware that the issue is probably with the 'y=value;' bit, but I really don't know any way to move foreward. The error message I get is 'Error using plot Invalid color or line style'. It is to be noted that when I only insert x into the textbox, I do get a function (with error lines I believe). See below: All help is appriciated.

0 Comments
Accepted Answer
Voss
on 18 Apr 2022
The reason using 'x' works is because 'x' is a valid line marker, so MATLAB plots x = -10:0.1:10 against its indices, 1:201 (note the X-axis values) using the marker 'x' (i.e., each point is plotted with an 'x' marker).
To get it to work properly for any function of x, you can make the user-input into a function using str2func and evaluate that function at your specified x values (-10:0.1:10):
ax = gca(); % an axes to plot into
x = -10:0.1:10; % fixed x values to plot over
value = 'x'; % user input
y = str2func(['@(x)' value]) % y is the anonymous function @(x)x
plot(ax, x, y(x)); % evaluate y at x and plot
value = 'x.^2'; % different user input
y = str2func(['@(x)' value]) % y is the anonymous function @(x)x.^2
plot(ax, x, y(x)); % evaluate y at x and plot
0 Comments
More Answers (0)
See Also
Categories
Find more on Line Plots 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!
