GUI use inputs as min and max for a function question? - Homework

I am making a GUI where it plots the cos(x) in the range of [0 10]. However, there are two open textboxes that can be changed by the user to create a new min and max range for cos(x). If the user changes these two numbers, the cos(x) plot should update accordingly.
Here is my script so far:
function varargout = costext(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @costext_OpeningFcn, ...
'gui_OutputFcn', @costext_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before costext is made visible.
function costext_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for costext
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes costext wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = costext_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
fplot('cos', [0, 1]);
xlabel('x');
ylabel('cos(x)');
function edit3_Callback(hObject, eventdata, handles)
function edit3_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit4_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
This works only to show the cos(x) function from [0 10] using fplot, but how can I change the MIN and MAX for what the user inputs and changes the graph???

2 comentarios

Edited .m and .fig is attached. The lim still doesn't work.
What is wrong with the GUI???
Attached files to the question above:

Iniciar sesión para comentar.

Respuestas (2)

Image Analyst
Image Analyst el 29 de Nov. de 2013
Editada: Image Analyst el 29 de Nov. de 2013
Try xlim() and ylim().
Referencing
fplot('cos', [0, 1]);
I am only changing the x values [XMIN XMAX] by the user. I was wondering how I can have the user input adjust the graph accordingly by changing the text on the GUI?

8 comentarios

You can have an edit field where the user types in something. Then you read it and call xlim() and/or ylim().
which function is best to read when using xlim?
That doesn't make sense. You get the user's input from the edit box by using get() then turn it into a number and send that number into xlim() to set the axes limits to whatever the user said.
a = get(edit2);
b = str2num(a);
xlim(b);
Using above script, would I use edit2 as the handle for the user input?
I get this error when I try to change the limits.
Undefined function or variable 'edit2'.
Error in costext>edit3_Callback (line 47)
a = get(edit2);
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in costext (line 17)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)costext('edit3_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Below is an updated version.
So when someone changes the text boxes, they become the new MIN and MAX. The left one is the MIN, the right one is the MAX.
How would I make it so that I get the inputted information and make it the new MIN and MAX?
And how does the fix I attached in my answer not do that? I believe it does.
I tried to change the MIN and MAX and I got this update.
Error using feval
Undefined function 'edit2_Callback' for
input arguments of type 'struct'.
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in costext (line 17)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)costext('edit2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Error using feval
Undefined function 'btnUpdate_Callback' for
input arguments of type 'struct'.
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in costext (line 17)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)costext('btnUpdate_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Error using feval
Undefined function 'edit2_Callback' for
input arguments of type 'struct'.
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in costext (line 17)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)costext('edit2_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
Also, I am trying to do this without using an update button, and just have the MIN and MAX change when the user presses enter.
???
Then you need to create callbacks for the edit boxes and put a call to UpdatePlot() in there. The disadvantage of doing that is that the user will have to know that it won't know when they're done typing in a number into the edit field and that nothing will happen until that edit field loses focus. So they need to click away , such as in the other edit field or on a button or on some other control. And if they're going to have to click someplace outside the edit field anyway , then why not just make it obvious with an "Update plot" button?

Iniciar sesión para comentar.

Categorías

Más información sobre Migrate GUIDE Apps en Centro de ayuda y File Exchange.

Preguntada:

el 29 de Nov. de 2013

Comentada:

el 3 de Dic. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by