function varargout = checkbox(varargin)
% CHECKBOX MATLAB code for checkbox.fig
%      CHECKBOX, by itself, creates a new CHECKBOX or raises the existing
%      singleton*.
%
%      H = CHECKBOX returns the handle to a new CHECKBOX or the handle to
%      the existing singleton*.
%
%      CHECKBOX('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in CHECKBOX.M with the given input arguments.
%
%      CHECKBOX('Property','Value',...) creates a new CHECKBOX or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before checkbox_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to checkbox_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 checkbox

% Last Modified by GUIDE v2.5 05-Oct-2015 17:18:46

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @checkbox_OpeningFcn, ...
                   'gui_OutputFcn',  @checkbox_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 checkbox is made visible.
function checkbox_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 checkbox (see VARARGIN)

Busnumber=7 %number of buses, here predefined

% Choose default command line output for Try1_bus
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

hBusGui = findobj('Tag','testfigure_')         %get the GUI figure
handlesGui_bus = guidata(hBusGui)
 
 %create 'Busnumber' checkboxes and initialize Checkboxes as false

 for i=1:Busnumber-1  %number buses in the network
     
     if i==1
         newRow={false   ['Bus', num2str(i)]};
         newData= [newRow];
         set( handlesGui_bus.uitable1_,'Data',newData);
     end
     
     oldData = get( handlesGui_bus.uitable1_,'Data');

     newRow={false   ['Bus', num2str(i+1)]};
     newData = [oldData; newRow];
     
     set( handlesGui_bus.uitable1_,'Data',newData)
 end
 
 display(handles)
 
 
 function uitable1__CellEditCallback(hObject, eventdata, handles)
% hObject    handle to uitable_ (see GCBO)
%   eventdata  structure with the following fields (see UITABLE)
%	Indices: row and column indices of the cell(s) edited
%	PreviousData: previous data for the cell(s) edited
%	EditData: string(s) entered by the user
%	NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
%	Error: error string when failed to convert EditData to appropriate value for Data
% handles    structure with handles and user data (see GUIDATA)

data=get(hObject,'Data');                                       % get the data cell array of the table
cols=get(hObject,'ColumnFormat');                               % get the column formats

if strcmp(cols(eventdata.Indices(2)),'logical')                 % if the column of the edited cell is logical
    if eventdata.EditData                                       % if the checkbox was set to true
        data{eventdata.Indices(1),eventdata.Indices(2)}=true;   % set the data value to true
    else                                                        % if the checkbox was set to false
        data{eventdata.Indices(1),eventdata.Indices(2)}=false;  % set the data value to false
    end
end

set(hObject,'Data',data); % now set the table's data to the updated data cell array 

eventdata.EditData; 
data=get(hObject,'Data');  
length(data)

%Find indices of cells that are marked 'true'
indices=[];
for i=1:length(data)
    if data{i,1}==1
        indices=[indices,i]; 
    end
end

% --- Outputs from this function are returned to the command line.
function varargout = checkbox_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 button press in select_all.
function select_all_Callback(hObject, eventdata, handles)
% hObject    handle to select_all (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 select_all

hBus_ui_Gui = findobj('Tag','uitable1_')         %get the GUI figure
handlesGui_bus = guidata(hBus_ui_Gui)

data=get(hBus_ui_Gui,'Data')                                       % get the data cell array of the table
[m,n]=size(data);

if get(hObject,'Value')==1

    for i=1:m
        data{i,1}=true;
    end

    set(hBus_ui_Gui,'Data',data)
    
end

set(handles.deselect_all,'Value',false);



% --- Executes on button press in deselect_all.
function deselect_all_Callback(hObject, eventdata, handles)
% hObject    handle to deselect_all (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 deselect_all

hBus_ui_Gui = findobj('Tag','uitable1_')         %get the GUI figure
handlesGui_bus = guidata(hBus_ui_Gui)

data=get(hBus_ui_Gui,'Data')                                       % get the data cell array of the table
[m,n]=size(data);

if get(hObject,'Value')==1
    for i=1:m
        data{i,1}=false;
    end

    set(hBus_ui_Gui,'Data',data)
end

set(handles.select_all,'Value',false);
