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 Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by