How to import equation from GUI to functions?
2 views (last 30 days)
Show older comments
I want the user to be able to insert his own equation (e.g. a*x.^2 + b*x + c) to be used to plot the function. I have already defined the variables a, b and c (to which it will be limited) and have everything working if I put y=*equation* in the .m file. How would I point Matlab towards using what is in the GUI text box (the equation input) to answer y=... and plot accordingly?
2 Comments
Accepted Answer
Matt Fig
on 25 Sep 2012
Here is an example. Paste the code into a new M-file and run it. In the text box you can enter an equation which depends on x, a, b and c. The values of a, b and c are hardwired as you indicate above.
function [] = plot_func()
% Help
S.fh = figure('units','pixels',...
'position',[300 300 200 130],...
'menubar','none',...
'name','plot_func',...
'numbertitle','off',...
'resize','off');
S.ed = uicontrol('style','edit',...
'unit','pix',...
'position',[10 70 180 40],...
'string','Enter func of x,a,b,c',...
'fontsize',10);
S.pb = uicontrol('style','push',...
'unit','pix',...
'position',[10 20 180 35],...
'string','plot it!',...
'fontsize',14);
set(S.pb,'callback',{@pb_call,S}) % Set callback.
uicontrol(S.ed)
function [] = pb_call(varargin)
% Callback for pushbutton.
S = varargin{3}; % Get the structure.
func = vectorize(inline(get(S.ed,'string'),'a','b','c','x'));
x = 0:.01:1;
figure
plot(x,func(2,3,4,x)) % a=2,b=3,c=4 - hardwired.
3 Comments
Matt Fig
on 26 Sep 2012
Edited: Matt Fig
on 26 Sep 2012
If you are using GUIDE, then you will want to have something that triggers the plot, like a pushbutton. In the callback for the pushbutton, you do what you did but add to it:
a = str2num(get(handles.a_value, 'String' ));
b = str2num(get(handles.b_value, 'String' ));
c = str2num(get(handles.c_value, 'String' ));
y = vectorize(inline(get(handles.y,'string'),'a','b','c','x'));
x = 0:.01:1;
figure
plot(x,y(a,b,c,x))
More Answers (0)
See Also
Categories
Find more on Migrate GUIDE Apps 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!