check box GUI and grapsh

6 views (last 30 days)
ADC
ADC on 19 Dec 2018
Edited: Walter Roberson on 26 Dec 2018
I'm back again, becouse I'm still working on my gui;
Now I've the following question:
In my gui I've placed 6 check box;
each check box if is actived shall plot a graphs;
Of course what I want is each time that I select a check box the graph shall plot data that coming from different matrix:
EX: given six matrix A1,A2,A3,A4,A5,A6
and six check box (c1,c2,c3,..c6) if for example I select c1 and c2 I want a graphs with both data;
I tried to create the condictions with a cicle 'If ' but I understood that this will be a little complicated becouse I have to consider 64 different condictions....(2 elevated to the sixth...)
is there any easiest way to do that???
  4 Comments
Sarah Crimi
Sarah Crimi on 19 Dec 2018
Edited: Sarah Crimi on 19 Dec 2018
You only want A1 through A6, right? Then, 64-6 solutions should be error messages. I think it is better to do radio buttons possibly because then you can use a radio button group in guide.
If you want to do checkboxes, you could do:
if(Sector1==1 & Sector2==0 & Serctor3==0 & Sector4==0 & Sector5==0 & Serctor6==0)
plot (A1(:,1),A1(:,2))
elseif(Sector1==0&Sector2==1 &&Serctor3==0 & Sector4==0 & Sector5==0 & Serctor6==0)
plot (A2(:,1),A2(:,2))
elseif (Sector1==0&Sector2==0 &&Serctor3==1 & Sector4==0 & Sector5==0 & Serctor6==0)
plot (A3(:,1),A3(:,2))
....etc.
else
msgbox('Error');
end
ADC
ADC on 20 Dec 2018
Not exactly, what I want obtain is when sectors are 1 a plot when is o no plots;
I mean if sector1==1 and sector4==1 and the rest is 0 i want obtain the graps of sector 1 and 4 togheter;
for this reason I've wrote 64 combination of posible graphs;
My question is
instead of write all condictions whit " if "and "elseif "is there a possible solution to avoid write every thing ?
becouse if I only think , if I would add another ceck box the combinations become 128....

Sign in to comment.

Accepted Answer

Luna
Luna on 20 Dec 2018
Edited: Luna on 20 Dec 2018
I recommend you write 6 callbacks for each checkbox, and check the value of checkbox.
So any time a checkbox is checked it plots to your figure, and any time it is unchecked it deletes the line you referenced.
function chkbox1_Callback(guiobj,chckboxObj,varargin)
if checkbox1Obj.value == 1
guiobj.line1 = plot(X1,Y1,'Parent',guiobj.figure); % also store your line variable, figure variable under the gui object
hold on; % hold it for any other check boxes pressed.
else
delete(guiobj.line1);
end
end
Note:
While creating your uicontrol element put callback property like below to get your guiobject as first input reference in above callback. The second input will be your checkbox object.
uicontrol('Parent', guiobj.figure, 'style', 'checkbox', 'Callback', @guiobj.chkbox1_Callback);
Note2:
You should have a classdef of guiobj which has properties of figure,line1,line2,..etc.
  2 Comments
ADC
ADC on 21 Dec 2018
thank Luna for your help it was useful for me;
What I did was define for each ceck box different condiction and seems work properly;I don't know the difference between
function chkbox1_Callback(guiobj,chckboxObj,varargin)
and
function checkbox1_Callback(hObject, eventdata, handles)%what I used
any way
now I have the problem that afther a plot if I put the command 'hold on' the graph fill up and I don't know clean it up;
here is my script
function varargout = WAT(varargin)
% WAT MATLAB code for WAT.fig
% WAT, by itself, creates a new WAT or raises the existing
% singleton*.
%
% H = WAT returns the handle to a new WAT or the handle to
% the existing singleton*.
%
% WAT('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in WAT.M with the given input arguments.
%
% WAT('Property','Value',...) creates a new WAT or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before WAT_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to WAT_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help WAT
% Last Modified by GUIDE v2.5 20-Dec-2018 14:11:11
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @WAT_OpeningFcn, ...
'gui_OutputFcn', @WAT_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 WAT is made visible.
function WAT_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to WAT (see VARARGIN)
% Choose default command line output for WAT
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes WAT wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = WAT_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
global A
i=get(handles.popupmenu1,'Value');
WTGi=A(A(:,4)==i,:);
h(1)=subplot(2,2,1)
plot(WTGi(:,8),WTGi(:,6),'x')
xlabel('Wind speed [m/s]')
ylabel('Power Production [kWh]')
title('Power Curve')
h(2)=subplot(2,2,2)
histogram(WTGi(:,8))
title('Wind distrubution')
xlabel('Wind speed')
h(3)=subplot(2,2,3)
rose(WTGi(:,11))
title('Wind Direction')
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject handle to checkbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox1
global A
SEC1=get(handles.checkbox1,'value')
SEC2=get(handles.checkbox2,'value')
SEC3=get(handles.checkbox3,'value')
SEC4=get(handles.checkbox4,'value')
SEC5=get(handles.checkbox5,'value')
SEC6=get(handles.checkbox6,'value')
i=get(handles.popupmenu1,'Value');
WTGi=A(A(:,4)==i,:);
WTGiSEC1=WTGi(WTGi(:,11)<60,:);
WTGiSEC2=WTGi(WTGi(:,11)>60 & WTGi(:,11)<120,:);
if SEC1==1
h(1)=subplot(2,2,1)
plot(WTGiSEC1(:,8),WTGiSEC1(:,6),'x');
if SEC1==1 & SEC2==1 | SEC3==1 | SEC4==1 | SEC5==1 | SEC6==1
hold on
plot(WTGiSEC1(:,8),WTGiSEC1(:,6),'x');
else
h(1)=subplot(2,2,1)
plot(0,0,'x')
end
end
% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
% hObject handle to checkbox2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global A
SEC1=get(handles.checkbox1,'value')
SEC2=get(handles.checkbox2,'value')
SEC3=get(handles.checkbox3,'value')
SEC4=get(handles.checkbox4,'value')
SEC5=get(handles.checkbox5,'value')
SEC6=get(handles.checkbox6,'value')
i=get(handles.popupmenu1,'Value');
i=get(handles.popupmenu1,'Value');
WTGi=A(A(:,4)==i,:);
WTGiSEC2=WTGi(WTGi(:,11)>60 & WTGi(:,11)<120,:)
if SEC2==1
h(1)=subplot(2,2,1)
plot(WTGiSEC2(:,8),WTGiSEC2(:,6),'x');
if SEC2==1 & SEC1==1 | SEC3==1 | SEC4==1 | SEC5==1 | SEC6==1
hold on
plot(WTGiSEC2(:,8),WTGiSEC2(:,6),'x');
else
h(1)=subplot(2,2,1)
plot(0,0,'x')
end
end
% Hint: get(hObject,'Value') returns toggle state of checkbox2
% --- Executes on button press in checkbox3.
function checkbox3_Callback(hObject, eventdata, handles)
% hObject handle to checkbox3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox3
global A
SEC1=get(handles.checkbox1,'value')
SEC2=get(handles.checkbox2,'value')
SEC3=get(handles.checkbox3,'value')
SEC4=get(handles.checkbox4,'value')
SEC5=get(handles.checkbox5,'value')
SEC6=get(handles.checkbox6,'value')
i=get(handles.popupmenu1,'Value');
i=get(handles.popupmenu1,'Value');
WTGi=A(A(:,4)==i,:);
WTGiSEC3=WTGi(WTGi(:,11)>120 & WTGi(:,11)<180,:);
if SEC3==1
h(1)=subplot(2,2,1)
plot(WTGiSEC3(:,8),WTGiSEC3(:,6),'x');
if SEC3==1 & SEC2==1 | SEC1==1 | SEC4==1 | SEC5==1 | SEC6==1
hold on
plot(WTGiSEC3(:,8),WTGiSEC3(:,6),'x');
else
h(1)=subplot(2,2,1)
plot(0,0,'x')
end
end
% --- Executes on button press in checkbox4.
function checkbox4_Callback(hObject, eventdata, handles)
% hObject handle to checkbox4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox4
global A
SEC1=get(handles.checkbox1,'value')
SEC2=get(handles.checkbox2,'value')
SEC3=get(handles.checkbox3,'value')
SEC4=get(handles.checkbox4,'value')
SEC5=get(handles.checkbox5,'value')
SEC6=get(handles.checkbox6,'value')
i=get(handles.popupmenu1,'Value');
WTGi=A(A(:,4)==i,:);
WTGiSEC4=WTGi(WTGi(:,11)>180 & WTGi(:,11)<240,:);
if SEC4==1
h(1)=subplot(2,2,1)
plot(WTGiSEC4(:,8),WTGiSEC4(:,6),'x')
if SEC4==1 & SEC2==1 | SEC3==1 | SEC1==1 | SEC5==1 | SEC6==1
hold on
plot(WTGiSEC4(:,8),WTGiSEC4(:,6),'x');
else
h(1)=subplot(2,2,1)
plot(0,0,'x')
end
end
% --- Executes on button press in checkbox6.
function checkbox6_Callback(hObject, eventdata, handles)
% hObject handle to checkbox6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox6
global A
SEC1=get(handles.checkbox1,'value')
SEC2=get(handles.checkbox2,'value')
SEC3=get(handles.checkbox3,'value')
SEC4=get(handles.checkbox4,'value')
SEC5=get(handles.checkbox5,'value')
SEC6=get(handles.checkbox6,'value')
i=get(handles.popupmenu1,'Value');
WTGi=A(A(:,4)==i,:);
WTGiSEC6=WTGi(WTGi(:,11)>300 & WTGi(:,11)<360,:);
if SEC6==1
h(1)=subplot(2,2,1)
plot(WTGiSEC6(:,8),WTGiSEC6(:,6),'x');
if SEC6==1 & SEC2==1 | SEC3==1 | SEC4==1 | SEC5==1 | SEC1==1
hold on
plot(WTGiSEC6(:,8),WTGiSEC6(:,6),'x');
else
h(1)=subplot(2,2,1)
plot(0,0,'x')
end
end
% --- Executes on button press in checkbox5.
function checkbox5_Callback(hObject, eventdata, handles)
% hObject handle to checkbox5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of checkbox5
global A
SEC1=get(handles.checkbox1,'value')
SEC2=get(handles.checkbox2,'value')
SEC3=get(handles.checkbox3,'value')
SEC4=get(handles.checkbox4,'value')
SEC5=get(handles.checkbox5,'value')
SEC6=get(handles.checkbox6,'value')
i=get(handles.popupmenu1,'Value');
WTGi=A(A(:,4)==i,:);
WTGiSEC5=WTGi(WTGi(:,11)>240 & WTGi(:,11)<300,:);
if SEC5==1
h(1)=subplot(2,2,1)
plot(WTGiSEC5(:,8),WTGiSEC5(:,6),'x');
if SEC5==1 & SEC2==1 | SEC3==1 | SEC4==1 | SEC1==1 | SEC6==1
hold on
plot(WTGiSEC5(:,8),WTGiSEC5(:,6),'x');
else
h(1)=subplot(2,2,1)
plot(0,0,'x')
end
end
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
A=xlsread(uigetfile);
global A
Luna
Luna on 26 Dec 2018
Let me explain the difference.
1) If you create your uicontrol's callback like below:
uicontrol(.. .., 'Callback', @guiObj.chkbox1_Callback)
and then define your callback as follows:
function chkbox1_Callback(guiObj,chckboxObj,eventdata)
% first input is guiObj which is the handle of main gui object(you can store any other user defined data to properties of guiObj )
% second input is chckboxObj which is the handle of checkbox uicontrol element. You can change its value (check, uncheck,enable,disable,..etc) and any other things.
% third input is eventdata (actiondata of )
end
2) If you create your uicontrol's callback like below:
uicontrol(.. .., 'Callback', @chkbox1_Callback)
and then define your callback as follows:
function checkbox1_Callback(hObject, eventdata, handles)%what I used
% first input is hObject which is your checkbox obj
% second input is eventdata which is the action data
% third is the handles which is the common handles of main gui some information can be stored in here to share data.
% if you are using GUIDE generally built-in guidata function you can use to store and get data whenever you wanted.
end
read this for more explanation:
I personally do not use GUIDE, so I prefer to do in the first option I have explained. I built my own structure and my own uicontrol elements one by one myself.
To delete your plot lines whenever the checkbox clicked, if it is unchecked, you should call a delete function. To do so, while plotting you should store the line you have plotted in somewhere you can always reach. (Like a common handle). Also I recommend you to store axes handle in some field, too. So you can manipulate the axes whenever you need.
function checbox_1_callback(hObj,event,handles)
if hObj.Value == 1
handles.Line1 = plot(..); % this stores your plotted line under handles.line1
else
delete(handles.Line1);
end
end
for me I store them under guiObj which I defined this class and its properties as I wish. You can choose whatever you want.

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!