GUI Checkboxes and plot button please help

1 view (last 30 days)
Hello,
I have two check boxes AB and CD and a plot button. AB and CD are array of numbers that I want to plot depending on which one is checked, and I would not like it to plot until I click the plot button. This is my code below, the problem is that it doesn't output anything and I get an error: Not enough input arguments. Please Help:
function varargout = Try1(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Try1_OpeningFcn, ...
'gui_OutputFcn', @Try1_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
end
function Try1_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
end
function varargout = Try1_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
end
function AB_box_Callback(hObject, eventdata, handles)
AB_status=get(handles.AB_box,'value');
end
function CD_box_Callback(hObject, eventdata, handles)
CD_status=get(handles.soc_box,'value');
end
function plot_button_Callback(hObject, eventdata, handles, v_status,soc_status)
AB = [5 5.7 10.3 15.7];
CD=[45 45 22 11];
if get(AB_status,value)==1
plot(AB);
elseif get(CD_status)==1
plot(CD);
end
end

Accepted Answer

Roberto
Roberto on 27 Apr 2014
There's always more than one way to do everything! It depends on what you want!! try this code and see if that's what you want to do... but again it depends on how your app is managing data, I used nested functions to keep handles of objects. But is not the only way to do it.. Good luck!
function myGUI
close all ; h.figure = figure ;
h.chk1 = uicontrol(h.figure,'Style','checkbox','Position',[5 5 200 20],'String','Some text','Value',1);
h.chk2 = uicontrol(h.figure,'Style','checkbox','Position',[5 25 200 20],'String','Other text','Value',1);
h.btn1 = uicontrol(h.figure,'Style','pushbutton','Position',[5 45 200 20],'String','plot','Callback',@btnCallback);
h.axes = axes('position',[.4 .05 .5 .9]);
AB = [5 5.7 10.3 15.7]; CD=[45 45 22 11];
function btnCallback(~,~)
cla ;
if get(h.chk1,'Value')==1
plot(AB);
elseif get(h.chk2,'Value')==1
plot(CD);
end
end
end

More Answers (2)

Roberto
Roberto on 27 Apr 2014
I see now... the problem... inthe 'get' function. the proper way of getting a property is:
get(handle,'Value') ;
So check this code:
function plot_button_Callback(hObject, eventdata, handles, v_status,soc_status)
AB = [5 5.7 10.3 15.7];
CD=[45 45 22 11];
if get(AB_status,'value')==1
plot(AB);
elseif get(CD_status,'value')==1
plot(CD);
end
end
  1 Comment
Sarah
Sarah on 27 Apr 2014
Thank you, but this still does not work the checkboxes do not have any output so when I click the plot button, it does not know which one I picked. Could you please show me how to get it to output?

Sign in to comment.


Jan
Jan on 27 Apr 2014
Edited: Jan on 27 Apr 2014
function AB_box_Callback(hObject, eventdata, handles)
handles = guidata(hObject);
handles.AB_status = get(handles.AB_box,'value');
handles.CD_status = 0;
guidata(hObject, handles);
end
function CD_box_Callback(hObject, eventdata, handles)
handles = guidata(hObject);
handles.AB_status = 0;
handles.CD_status = get(handles.soc_box,'value');
guidata(hObject, handles);
end
function plot_button_Callback(hObject, eventdata, handles, v_status,soc_status)
handles = guidata(hObject);
AB = [5 5.7 10.3 15.7];
CD=[45 45 22 11];
if handles.AB_status
plot(AB);
elseif handles.CD_status
plot(CD);
end
end
You wrote "get(AB_status,value)==1", but here the get() command is not used as explained in the documentation. The first argument must be an HG-handle, but in your code a local variable should be provided.
Nevertheless, AB_status is not a local variable. It was created inside the function AB_box_Callback. But variables exist in the current workspace only and as soon as this function is left, the local variables are cleared also. Therefore you need to store it persistently and for this job guidata is applied usually.
You find a bunch of equivalent discussion when you search for "share data in gui" in this forum. Searching in the forum is recommended before asking a question.
  2 Comments
Sarah
Sarah on 27 Apr 2014
I did search without success, I am new into matlab, if anyone can help with this I would really appreciate it.
Roberto
Roberto on 27 Apr 2014
Edited: Roberto on 27 Apr 2014
try this video tutorials: here follow it step-by-step, that's how I started

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!