How to import equation from GUI to functions?

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

Are there 3 editboxes in the GUI (for a, b and c)? And where does x come from?
Yeah, 3 seperate boxes. x is defined as an interval specified on the gui:
x= str2num(get(handles.x_lower, 'String' )):str2num(get(handles.x_upper, 'String' ));

Sign in to comment.

 Accepted Answer

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

John
John on 26 Sep 2012
Edited: John on 26 Sep 2012
Wow... Just so you know, I'm new at this. What I'm doing now, is using "guide" to create my gui and using input values like this:
a = str2num(get(handles.a_value, 'String' ));
with everything under function plot_Callback.
I don't know how to integrate your code above with how I'm doing it - it probably diffirs because I use the gui editor instead of raw code...?
Isn't there a way to just 'copy' the string from a text box in the gui into the code because everything works fine if I define y myself (e.g. y = a*x.^2 + b*x + c in my code). So if I can say y = whatever is in the string of equation text box, wouldn't that work?
Also, for now I'm not bothered with efficiency, e.g. using varargin, because I haven't really looked up on how it diffirs from what I'm doing now and this is a very small project. Thanks for the help so far! Still trying to build my program around your code now.
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))
PRAJWAL GOWDA HM comments to John:
can u send this program...

Sign in to comment.

More Answers (0)

Tags

Asked:

on 25 Sep 2012

Commented:

on 19 Jul 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!