How to import equation from GUI to functions?

2 ビュー (過去 30 日間)
John
John 2012 年 9 月 25 日
コメント済み: Walter Roberson 2019 年 7 月 19 日
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 件のコメント
Tom
Tom 2012 年 9 月 25 日
Are there 3 editboxes in the GUI (for a, b and c)? And where does x come from?
John
John 2012 年 9 月 26 日
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' ));

サインインしてコメントする。

採用された回答

Matt Fig
Matt Fig 2012 年 9 月 25 日
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 件のコメント
Matt Fig
Matt Fig 2012 年 9 月 26 日
編集済み: Matt Fig 2012 年 9 月 26 日
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))
Walter Roberson
Walter Roberson 2019 年 7 月 19 日
PRAJWAL GOWDA HM comments to John:
can u send this program...

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by