GUI Checkboxes and plot button please help

조회 수: 1 (최근 30일)
Sarah
Sarah 2014년 4월 27일
댓글: Sarah 2014년 4월 27일
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

채택된 답변

Roberto
Roberto 2014년 4월 27일
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
  댓글 수: 1
Sarah
Sarah 2014년 4월 27일
This works! Thank you very much!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Roberto
Roberto 2014년 4월 27일
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
Sarah
Sarah 2014년 4월 27일
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?

댓글을 달려면 로그인하십시오.


Jan
Jan 2014년 4월 27일
편집: Jan 2014년 4월 27일
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
Sarah
Sarah 2014년 4월 27일
I did search without success, I am new into matlab, if anyone can help with this I would really appreciate it.
Roberto
Roberto 2014년 4월 27일
편집: Roberto 2014년 4월 27일
try this video tutorials: here follow it step-by-step, that's how I started

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by