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

% Last Modified by GUIDE v2.5 26-Apr-2012 18:19:33

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ncsAnalyzer_OpeningFcn, ...
                   'gui_OutputFcn',  @ncsAnalyzer_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 ncsAnalyzer is made visible.
function ncsAnalyzer_OpeningFcn(hObject, eventdata, handles, varargin) %#ok<*INUSL>
% 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 ncsAnalyzer (see VARARGIN)

    handles.ncsEditor = varargin{1};
    handles.ncs = varargin{2};
% Obtain handles using GUIDATA with the caller's handle
%mainHandles = guidata(handles.ncsEditor);

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

    if strcmp(handles.ncs.ctrlType,'D-LTI Dynamic Feedback')
        set(handles.MatiMadButton,'Enable','off')
    end


% Position to be relative to parent:
% parentPosition = getpixelposition(handles.ncsEditor);
% currentPosition = get(hObject, 'Position');
% Sets the position to be directly centered on the main figure
% newX = parentPosition(1)+ round(parentPosition(3)/2 );
% newY = parentPosition(2)+ round(parentPosition(4)/2);
% newW = currentPosition(3);
% newH = currentPosition(4);
%
% %msgbox([num2str(newX) ',  ' num2str(newY) ',  ' num2str(newW) ',  ' num2str(newH)])
%
% set(hObject, 'Position', [newX, newY, newW, newH]);

% Update handles structure
    guidata(hObject, handles);


% --- Outputs from this function are returned to the command line.
function varargout = ncsAnalyzer_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;
%    delete(hObject);

% --- Executes on selection change in popupmenu13.
function popupmenu13_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu13 (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 popupmenu13 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu13


% --- Executes during object creation, after setting all properties.
function popupmenu13_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu13 (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



function edit31_Callback(hObject, eventdata, handles)
% hObject    handle to edit31 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit31 as text
%        str2double(get(hObject,'String')) returns contents of edit31 as a double


% --- Executes during object creation, after setting all properties.
function edit31_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit31 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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 selection change in popupmenu14.
function popupmenu14_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu14 (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 popupmenu14 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu14


% --- Executes during object creation, after setting all properties.
function popupmenu14_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu14 (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 selection change in popupmenu15.
function popupmenu15_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu15 (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 popupmenu15 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu15


% --- Executes during object creation, after setting all properties.
function popupmenu15_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu15 (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



function JnfGamma_Callback(hObject, eventdata, handles)
% hObject    handle to JnfGamma (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of JnfGamma as text
%        str2double(get(hObject,'String')) returns contents of JnfGamma as a double


function edit34_Callback(hObject, eventdata, handles)
% hObject    handle to edit34 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit34 as text
%        str2double(get(hObject,'String')) returns contents of edit34 as a double


% --- Executes during object creation, after setting all properties.
function edit34_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit34 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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


function GnbMaxGpts_Callback(hObject, eventdata, handles)
% hObject    handle to GnbMaxGpts (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of GnbMaxGpts as text
%        str2double(get(hObject,'String')) returns contents of GnbMaxGpts as a double


% --- Executes during object creation, after setting all properties.
function GnbMaxGpts_CreateFcn(hObject, eventdata, handles)
% hObject    handle to GnbMaxGpts (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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



function GnbTightness_Callback(hObject, eventdata, handles)
% hObject    handle to GnbTightness (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of GnbTightness as text
%        str2double(get(hObject,'String')) returns contents of GnbTightness as a double


% --- Executes during object creation, after setting all properties.
function GnbTightness_CreateFcn(hObject, eventdata, handles)
% hObject    handle to GnbTightness (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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 selection change in GnbUwired.
function GnbUwired_Callback(hObject, eventdata, handles)
% hObject    handle to GnbUwired (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 GnbUwired contents as cell array
%        contents{get(hObject,'Value')} returns selected item from GnbUwired


% --- Executes during object creation, after setting all properties.
function GnbUwired_CreateFcn(hObject, eventdata, handles)
% hObject    handle to GnbUwired (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 StabilityButton.
function StabilityButton_Callback(hObject, eventdata, handles)
% hObject    handle to StabilityButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

    set(hObject,'Enable','off');
    set(handles.VerticesButton,'Enable','off');
    set(handles.MatiMadButton,'Enable','off');
    drawnow;

    if get(handles.JNFradio,'Value') == 1

        [stable,verts] = isNcsStable(handles.ncs,'JNF',[]);

        numVerts= size(verts.Ab,2);
        set(handles.JnfVerts,'String',num2str(numVerts));
        if stable ==1
            set(handles.JnfStabResult,'String','Guaranteed')
            set(handles.JnfStabResult,'ForegroundColor','green')
        else
            set(handles.JnfStabResult,'String','Not Guaranteed')
            set(handles.JnfStabResult,'ForegroundColor','red')
        end

    elseif get(handles.CHradio,'Value') == 1


        [stable,verts] = isNcsStable(handles.ncs,'CH',[]);

        numVerts= size(verts.Ab,2);
        set(handles.ChVerts,'String',num2str(numVerts));
        if stable ==1
            set(handles.ChStabResult,'String','Guaranteed')
            set(handles.ChStabResult,'ForegroundColor','green')
        else
            set(handles.ChStabResult,'String','Not Guaranteed')
            set(handles.ChStabResult,'ForegroundColor','red')
        end

    elseif get(handles.GNBradio,'Value') == 1

        MaxGpts = get(handles.GnbMaxGpts,'String');
        tightness = get(handles.GnbTightness,'String');

        [stable,verts] = isNcsStable(handles.ncs,'GNB',[],str2double(MaxGpts),str2double(tightness));

        numVerts= size(verts.Ab,2);
        set(handles.GnbVerts,'String',num2str(numVerts));
        set(handles.GnbTightResult,'String',num2str(verts.MaxEps));

        if stable ==1
            set(handles.GnbStabResult,'String','Guaranteed')
            set(handles.GnbStabResult,'ForegroundColor','green')
        else
            set(handles.GnbStabResult,'String','Not Guaranteed')
            set(handles.GnbStabResult,'ForegroundColor','red')
        end

    end

    set(hObject,'Enable','on');
    set(handles.VerticesButton,'Enable','on');
    if strcmp(handles.ncs.ctrlType,'D-LTI Dynamic Feedback')
        set(handles.MatiMadButton,'Enable','off');
    else
        set(handles.MatiMadButton,'Enable','on');
    end


% --- Executes on selection change in JnfLyapType.
function JnfLyapType_Callback(hObject, eventdata, handles)
% hObject    handle to JnfLyapType (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 JnfLyapType contents as cell array
%        contents{get(hObject,'Value')} returns selected item from JnfLyapType


% --- Executes during object creation, after setting all properties.
function JnfLyapType_CreateFcn(hObject, eventdata, handles)
% hObject    handle to JnfLyapType (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



function ChGamma_Callback(hObject, eventdata, handles)
% hObject    handle to ChGamma (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ChGamma as text
%        str2double(get(hObject,'String')) returns contents of ChGamma as a double


% --- Executes during object creation, after setting all properties.
function ChGamma_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ChGamma (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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 selection change in ChLyapType.
function ChLyapType_Callback(hObject, eventdata, handles)
% hObject    handle to ChLyapType (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 ChLyapType contents as cell array
%        contents{get(hObject,'Value')} returns selected item from ChLyapType


% --- Executes during object creation, after setting all properties.
function ChLyapType_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ChLyapType (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 when user attempts to close ncsAnalyzer.
function ncsAnalyzer_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to ncsAnalyzer (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
set(handles.ncsEditor,'Visible','on');
delete(hObject);

% --- Executes during object deletion, before destroying properties.
function uipanel3_DeleteFcn(hObject, eventdata, handles)
% hObject    handle to uipanel3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes during object deletion, before destroying properties.
function ncsAnalyzer_DeleteFcn(hObject, eventdata, handles)
% hObject    handle to ncsAnalyzer (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes during object creation, after setting all properties.
function uipanel3_CreateFcn(hObject, eventdata, handles) %#ok<*INUSD,*DEFNU>
% hObject    handle to uipanel3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes during object creation, after setting all properties.
function ncsAnalyzer_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ncsAnalyzer (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes on button press in VerticesButton.
function VerticesButton_Callback(hObject, eventdata, handles)
% hObject    handle to VerticesButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

    set(hObject,'Enable','off');
    set(handles.StabilityButton,'Enable','off');
    set(handles.MatiMadButton,'Enable','off');
    drawnow;

    DtMdl = genDtNcsModel(handles.ncs);
    if get(handles.JNFradio,'Value')==1
        ovrAprx = genPolyOvrAprx(DtMdl,'JNF');
        assignin('base', 'ovrAprxJNF', ovrAprx);
    elseif get(handles.CHradio,'Value')==1
        ovrAprx = genPolyOvrAprx(DtMdl,'CH');
        assignin('base', 'ovrAprxCH', ovrAprx);
    elseif get(handles.GNBradio,'Value')==1

        MaxGpts = get(handles.GnbMaxGpts,'String');
        tightness = get(handles.GnbTightness,'String');

        ovrAprx = genPolyOvrAprx(DtMdl,'GNB',str2double(MaxGpts),str2double(tightness),true);
        assignin('base', 'ovrAprxGNB', ovrAprx);
    elseif get(handles.HYBradio,'Value')==1

    end

    set(hObject,'Enable','on');
    set(handles.StabilityButton,'Enable','on');
    set(handles.MatiMadButton,'Enable','on');


% --- Executes when selected object is changed in uipanel3.
function uipanel3_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uipanel3
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)

    if get(handles.JNFradio,'Value')==1
        set(handles.GnbMaxGpts,'Enable','off');
        set(handles.GnbTightness,'Enable','off');
    elseif get(handles.CHradio,'Value')==1
        set(handles.GnbMaxGpts,'Enable','off');
        set(handles.GnbTightness,'Enable','off');
    elseif get(handles.GNBradio,'Value')==1
        set(handles.GnbMaxGpts,'Enable','on');
        set(handles.GnbTightness,'Enable','on');
    elseif get(handles.HYBradio,'Value')==1
        set(handles.GnbMaxGpts,'Enable','off');
        set(handles.GnbTightness,'Enable','off');
    end


% --- Executes on button press in MatiMadButton.
function MatiMadButton_Callback(hObject, eventdata, handles)
% hObject    handle to MatiMadButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

    %[MATI,MAD]=hyb_main(handles.ncs);
    [MATI,MAD]=handles.ncs.findMatiMadBoundry();;
    figure;
    plot(MATI,MAD);
    ylabel('Mamimum Allowable Delay (MAD)');
    xlabel('Mamimum Allowable Transmission Interval (MATI)');


% --- Executes on button press in backButton.
function backButton_Callback(hObject, eventdata, handles)
% hObject    handle to backButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
    ncsAnalyzer_CloseRequestFcn(handles.ncsAnalyzer, eventdata, handles)
Index exceeds matrix dimensions.

Error in ncsAnalyzer>ncsAnalyzer_OpeningFcn (line 57)
    handles.ncsEditor = varargin{1};

Error in gui_mainfcn (line 221)
    feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});

Error in ncsAnalyzer (line 42)
    gui_mainfcn(gui_State, varargin{:});