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

% Last Modified by GUIDE v2.5 24-Jun-2004 15:23:08

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @EigenValuesFigure_OpeningFcn, ...
                   'gui_OutputFcn',  @EigenValuesFigure_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(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 EigenValuesFigure is made visible.
function EigenValuesFigure_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 EigenValuesFigure (see VARARGIN)

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

handles.baseFigure=varargin{1};

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes EigenValuesFigure wait for user response (see UIRESUME)
% uiwait(handles.figure1);
global SERIES
global SERIESINDEX
global SERIESNAMES
global SERIESTYPE
global SERIESSTAT

handles2=guidata(handles.baseFigure);
index=get(handles2.listbox1,'Value');
SRC=SERIES{index};

[m,n]=size(SRC);
[lMin,lMax]=lambda(m);
set(handles.textLambdaMin,'String',num2str(lMin));
set(handles.textLambdaMax,'String',num2str(lMax));
if abs(m-n)==1
   SRC=sortRowsByFirstCol(SRC);
   SERIES{index}=SRC;
end
[m,n]=size(SRC);
for i=1:m
    index=floor(SRC(i,2));
    S(i,:)=[blanks_w2([num2str(i) '.'],5) blanks_w2(num2str(SRC(i,1)),20)];
end
set(handles.listboxEigenValues,'String',S);
listboxEigenValues_Callback(hObject, eventdata, handles);

% --- Outputs from this function are returned to the command line.
function varargout = EigenValuesFigure_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 during object creation, after setting all properties.
function listboxEigenValues_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listboxEigenValues (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in listboxEigenValues.
function listboxEigenValues_Callback(hObject, eventdata, handles)
% hObject    handle to listboxEigenValues (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns listboxEigenValues contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listboxEigenValues

global SERIES
global SERIESINDEX
global SERIESNAMES
global SERIESTYPE
global SERIESSTAT

handles2=guidata(handles.baseFigure);
index=get(handles2.listbox1,'Value');
SRC=SERIES{index};
[m,n]=size(SRC);
NAMES=SERIESNAMES{index};
indexVector=get(handles.listboxEigenValues,'Value');

[m,n]=size(SRC);
VECTOR=SRC(indexVector,3:n);
VECTOR=VECTOR';
VECTOR=[VECTOR VECTOR];
VECTOR=sortRowsByFirstCol(VECTOR);
for i=1:m
    index=floor(VECTOR(i,2));
    S(i,:)=[blanks_w2(num2str(VECTOR(i,1)),15) blanks_w2(char(NAMES{index,2}),30) blanks_w2(char(NAMES{index,1}),40)];
end
set(handles.listboxEigenVector,'String',S);

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

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in listboxEigenVector.
function listboxEigenVector_Callback(hObject, eventdata, handles)
% hObject    handle to listboxEigenVector (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns listboxEigenVector contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listboxEigenVector


% --- Executes during object creation, after setting all properties.
function editStartNumber_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editStartNumber (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function editStartNumber_Callback(hObject, eventdata, handles)
% hObject    handle to editStartNumber (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 editStartNumber as text
%        str2double(get(hObject,'String')) returns contents of editStartNumber as a double


% --- Executes during object creation, after setting all properties.
function editEndNumber_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editEndNumber (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function editEndNumber_Callback(hObject, eventdata, handles)
% hObject    handle to editEndNumber (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 editEndNumber as text
%        str2double(get(hObject,'String')) returns contents of editEndNumber as a double


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

global SERIES
global SERIESINDEX
global SERIESNAMES
global SERIESTYPE
global SERIESSTAT

handles2=guidata(handles.baseFigure);
index=get(handles2.listbox1,'Value');
SRC=SERIES{index};
[m,n]=size(SRC);
NAMES=SERIESNAMES{index};
indexVector=get(handles.listboxEigenValues,'Value');

[m,n]=size(SRC);
VECTOR=SRC(indexVector,3:n);
VECTOR=VECTOR';
VECTOR=[VECTOR VECTOR];
VECTOR=sortRowsByFirstCol(VECTOR);
startIndex=str2num(get(handles.editStartNumber,'String'));
endIndex=str2num(get(handles.editEndNumber,'String'));

filename=SERIESSTAT{index};
filename=filename.fileName;
[m,n]=size(filename);
i=n;
while filename(1,i)~='\'
    i=i-1;
end
filename=[filename(1,1:i) num2str(SRC(indexVector,1)) '.txt'];
fid=fopen(filename,'w');
for i=startIndex:endIndex
    index=floor(VECTOR(i,2));    
    STRING=[blanks_w2(num2str(VECTOR(i,1)),15) blanks_w2(char(NAMES{index,2}),15) blanks_w2(char(NAMES{index,1}),40)];
    fprintf(fid,'%s\n',STRING);
end
fclose(fid);


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

global SERIES
global SERIESINDEX
global SERIESNAMES
global SERIESTYPE
global SERIESSTAT

handles2=guidata(handles.baseFigure);
index=get(handles2.listbox1,'Value');
SRC=SERIES{index};
SRC=SRC(:,1);
SRC=SRC';

SERIESINDEX=SERIESINDEX+1;
SERIES{SERIESINDEX}=SRC;
SERIESTYPE{SERIESINDEX}=[char(SERIESTYPE{index}) ';EIG'];
SERIESSTAT{SERIESINDEX}=getStat(SRC);
SERIESSTAT{SERIESINDEX}.fileName=SERIESSTAT{index}.fileName;
saveInfo(handles2,SERIESSTAT{SERIESINDEX});
CD=get(handles2.listbox1,'String');
[CDm,CDn]=size(CD);
CD(CDm+1,:)='СобсЗнач';
set(handles2.listbox1,'String',CD);
set(handles2.listbox1,'Value',SERIESINDEX);

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

global SERIES
global SERIESINDEX
global SERIESNAMES
global SERIESTYPE
global SERIESSTAT

handles2=guidata(handles.baseFigure);
index=get(handles2.listbox1,'Value');
index2=get(handles.listboxEigenValues,'Value');
SRC=SERIES{index};
[m,n]=size(SRC);
SRC=SRC(index2,3:n);

SERIESINDEX=SERIESINDEX+1;
SERIES{SERIESINDEX}=SRC;
SERIESTYPE{SERIESINDEX}=[char(SERIESTYPE{index}) ';EVC'];
SERIESSTAT{SERIESINDEX}=getStat(SRC);
SERIESSTAT{SERIESINDEX}.fileName=SERIESSTAT{index}.fileName;
saveInfo(handles2,SERIESSTAT{SERIESINDEX});
CD=get(handles2.listbox1,'String');
[CDm,CDn]=size(CD);
CD(CDm+1,:)='СобсВект';
set(handles2.listbox1,'String',CD);
set(handles2.listbox1,'Value',SERIESINDEX);
