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

% Last Modified by GUIDE v2.5 27-Jun-2011 10:53:42

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

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

% Update handles structure
guidata(hObject, handles);

global SERIES
global SERIESINDEX
global SERIESTYPE
global SERIESSTAT

SERIESINDEX=0;

% UIWAIT makes work2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);

%========================================================================================================================
% --- Outputs from this function are returned to the command line.
function varargout = work2_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;


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

[filename,pathname,filterindex]=uigetfile(...
    {'*.*','Все файлы (*.*)'}, ...
     'Выберите файл','');
try
    SRC=load([pathname '\' filename]);
catch
    return
end
[m,n]=size(SRC);
isSingle=1;
if (m~=1) && (n~=1)
    isSingle=0;
    [m2,n2]=size(filename);
    filename2=[filename(1,1:n2-4) '.nm'];
    [SRCNames,SRCSectors]=textread([pathname '\' filename2],'%s%s','delimiter','\t');
end
%if m>n
if (m~=1) && (isSingle==1)                                                                                 % !CHNG!
    SRC=SRC';
elseif (isSingle==0)
    ROW=0; COL=0;
    for i=2:5
        ROW=ROW+abs(SRC(1,i)-SRC(1,i-1));
        COL=COL+abs(SRC(i,1)-SRC(i-1,1));
    end
    [ROW,COL]=size(SRC);
    if ROW>COL
        SRC=SRC';
    end
end
%if m>1
%    warndlg('Не отредактированная база. Отредактируйте перед чтением','Ошибка при чтении.');
%    return
%end
SRC=change_fromwork2(SRC);

global SERIES
global SERIESNAMES
global SERIESINDEX
global SERIESTYPE
global SERIESSTAT

if (get(handles.checkbox1,'Value')==get(handles.checkbox1,'Max')) && (isSingle==1)
    FigureSrc=figure;
    plot(SRC,'.');
end

SERIESINDEX=SERIESINDEX+1;
SERIES{SERIESINDEX}=SRC;
SERIESTYPE{SERIESINDEX}='MRC';
STATTemp=getStat(SRC);
STATTemp.fileName=[pathname filename];
SERIESSTAT{SERIESINDEX}=STATTemp;
saveInfo(handles,SERIESSTAT{SERIESINDEX});
CD=get(handles.listbox1,'String');
[CDm,CDn]=size(CD);

if (isSingle==0)
    SERIESNAMES{SERIESINDEX}=[SRCNames SRCSectors];
    set(handles.listbox1,'String',[CD; 'Матрица ']);
else
   set(handles.listbox1,'String',[CD; 'Исходный']);
end

set(handles.listbox1,'Value',SERIESINDEX);

%========================================================================================================================
% --------------------------------------------------------------------
function MenuBasesItemSave_Callback(hObject, eventdata, handles)
% hObject    handle to MenuBasesItemSave (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 SERIESTYPE
global SERIESSTAT

[filename,pathname,filterindex]=uiputfile(...
    {'*.txt','Текстовые файлы (*.txt)'}, ...
     'Выберите файл','');

[m,n]=size(filename);
if (n<4) || (~((filename(n-3)=='.') && (filename(n-2)=='t') && (filename(n-1)=='x') && (filename(n)=='t')))
    filename=[filename '.txt'];
end
 
SRC=SERIES{get(handles.listbox1,'Value')};
SRC=SRC';
NAME=[pathname filename];
save(NAME,'SRC','-ASCII');

%========================================================================================================================
% --------------------------------------------------------------------
function MenuProceedItemReturns_Callback(hObject, eventdata, handles)
% hObject    handle to MenuProceedItemReturns (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 SERIESTYPE
global SERIESSTAT

% Вычисляем возвраты матрицы, выделенной в ListBox
index=get(handles.listbox1,'Value');
SRC=SERIES{index};
RES=getReturn(SRC);

SERIESINDEX=SERIESINDEX+1;
SERIES{SERIESINDEX}=RES;
SERIESTYPE{SERIESINDEX}=[char(SERIESTYPE{index}) ';RET'];
SERIESSTAT{SERIESINDEX}=getStat(RES);
SERIESSTAT{SERIESINDEX}.fileName=SERIESSTAT{index}.fileName;
saveInfo(handles,SERIESSTAT{SERIESINDEX});
CD=get(handles.listbox1,'String');
[CDm,CDn]=size(CD);
CD(CDm+1,:)='Возвраты';
set(handles.listbox1,'String',CD);
set(handles.listbox1,'Value',SERIESINDEX);

if get(handles.checkbox1,'Value')==get(handles.checkbox1,'Max')
    figureTemp=figure;
    plot(RES);
end

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

VolatilityFigure(handles.figure1);

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

ACorrFigure(handles.figure1);

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

HurstFigure(handles.figure1);

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

DFAFigure(handles.figure1);

%========================================================================================================================
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox1 (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 listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject    handle to listbox1 (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 listbox1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox1

global SERIES
global SERIESINDEX
global SERIESTYPE
global SERIESSTAT

index=get(handles.listbox1,'Value');
if (SERIESINDEX>=index)
    saveInfo(handles,SERIESSTAT{index});
end

%========================================================================================================================
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (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 SERIESTYPE
%global SERIESSTAT
%
%figure;
%plot(SERIES{get(handles.listbox1,'Value')});
PlotFigure(handles.figure1);

%========================================================================================================================
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (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 SERIESTYPE
global SERIESSTAT

index=get(handles.listbox1,'Value');
TSLabels=get(handles.listbox1,'String');
if index==SERIESINDEX
    set(handles.listbox1,'Value',index-1);
else
    for i=index:SERIESINDEX-1
        SERIES{i}=SERIES{i+1};
        TSLabels(i,:)=TSLabels(i+1,:);
    end
end

TSLabels=TSLabels(1:SERIESINDEX-1,:);
set(handles.listbox1,'String',TSLabels);
SERIESINDEX=SERIESINDEX-1;
saveInfo(handles,SERIESSTAT{get(handles.listbox1,'Value')});

% --------------------------------------------------------------------
function MenuProceedItemAbsolute_Callback(hObject, eventdata, handles)
% hObject    handle to MenuProceedItemAbsolute (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 SERIESTYPE
global SERIESSTAT

index=get(handles.listbox1,'Value');
SRC=SERIES{index};
RES=abs(SRC);

SERIESINDEX=SERIESINDEX+1;
SERIES{SERIESINDEX}=RES;
SERIESSTAT{SERIESINDEX}=getStat(RES);
SERIESSTAT{SERIESINDEX}.fileName=SERIESSTAT{index}.fileName;
SERIESTYPE{SERIESINDEX}=[char(SERIESTYPE{index}) ';ABS'];
saveInfo(handles,SERIESSTAT{SERIESINDEX});
CD=get(handles.listbox1,'String');
[CDm,CDn]=size(CD);
CD(CDm+1,:)='Модуль  ';
set(handles.listbox1,'String',CD);
set(handles.listbox1,'Value',SERIESINDEX);

if get(handles.checkbox1,'Value')==get(handles.checkbox1,'Max')
    figure;
    plot(RES);
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


% --------------------------------------------------------------------
function MenuProceedItemDetrend_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_1 (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 SERIESTYPE
global SERIESSTAT

index=get(handles.listbox1,'Value');
SRC=SERIES{index};
%!!!!!!!!!!!!!!!!!!!!!!!!!!!RES=detrend(SRC);
RES=detr(SRC);

SERIESINDEX=SERIESINDEX+1;
SERIES{SERIESINDEX}=RES;
SERIESSTAT{SERIESINDEX}=getStat(RES);
SERIESSTAT{SERIESINDEX}.fileName=SERIESSTAT{index}.fileName;
SERIESTYPE{SERIESINDEX}=[char(SERIESTYPE{index}) ';DET'];
saveInfo(handles,SERIESSTAT{SERIESINDEX});
CD=get(handles.listbox1,'String');
[CDm,CDn]=size(CD);
CD(CDm+1,:)='Детренд ';
set(handles.listbox1,'String',CD);
set(handles.listbox1,'Value',SERIESINDEX);

if get(handles.checkbox1,'Value')==get(handles.checkbox1,'Max')
    figure;
    plot(RES);
end


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

localDFAFigure(handles.figure1);



% --------------------------------------------------------------------
function MenuProceedItemShuffle_Callback(hObject, eventdata, handles)
% hObject    handle to MenuProceedItemShuffle (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 SERIESTYPE
global SERIESSTAT

index=get(handles.listbox1,'Value');
SRC=SERIES{index};
%!!!!!!!!!!!!!!!!!!!!!!!!!!!RES=detrend(SRC);
RES=shuffleMatrix(SRC);

SERIESINDEX=SERIESINDEX+1;
SERIES{SERIESINDEX}=RES;
SERIESSTAT{SERIESINDEX}=getStat(RES);
SERIESSTAT{SERIESINDEX}.fileName=SERIESSTAT{index}.fileName;
SERIESTYPE{SERIESINDEX}=[char(SERIESTYPE{index}) ';SHF'];
saveInfo(handles,SERIESSTAT{SERIESINDEX});
CD=get(handles.listbox1,'String');
[CDm,CDn]=size(CD);
CD(CDm+1,:)='Перемеш ';
set(handles.listbox1,'String',CD);
set(handles.listbox1,'Value',SERIESINDEX);

if get(handles.checkbox1,'Value')==get(handles.checkbox1,'Max')
    figure;
    plot(RES);
end


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

probDensityFigure(handles.figure1);


% --------------------------------------------------------------------
function MenuProceedItemNormalReturns_Callback(hObject, eventdata, handles)
% hObject    handle to MenuProceedItemNormalReturns (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 SERIESTYPE
global SERIESSTAT

% Вычисляем возвраты матрицы, выделенной в ListBox
index=get(handles.listbox1,'Value');
SRC=SERIES{index};
RES=normalisation(SRC);

SERIESINDEX=SERIESINDEX+1;
SERIES{SERIESINDEX}=RES;
SERIESTYPE{SERIESINDEX}=[char(SERIESTYPE{index}) ';RTN'];
SERIESSTAT{SERIESINDEX}=getStat(RES);
SERIESSTAT{SERIESINDEX}.fileName=SERIESSTAT{index}.fileName;
saveInfo(handles,SERIESSTAT{SERIESINDEX});
CD=get(handles.listbox1,'String');
[CDm,CDn]=size(CD);
CD(CDm+1,:)='ВозврНор';
set(handles.listbox1,'String',CD);
set(handles.listbox1,'Value',SERIESINDEX);

if get(handles.checkbox1,'Value')==get(handles.checkbox1,'Max')
    figureTemp=figure;
    plot(RES);
end


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

smoothFigure(handles.figure1);


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

MFDFAFigure(handles.figure1);


% --------------------------------------------------------------------
function BuildCCMatrtix_Callback(hObject, eventdata, handles)
% hObject    handle to BuildCCMatrtix (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 SERIESNAMES
global SERIESINDEX
global SERIESTYPE
global SERIESSTAT

% Вычисляем матрицу кросс-кореляций для матрицы, выделенной в ListBox
index=get(handles.listbox1,'Value');
SRC=SERIES{index};
RES=buildCHandle(SRC);

SERIESINDEX=SERIESINDEX+1;
SERIESNAMES{SERIESINDEX}=SERIESNAMES{index};
SERIES{SERIESINDEX}=RES;
SERIESTYPE{SERIESINDEX}=[char(SERIESTYPE{index}) ';CCM'];
SERIESSTAT{SERIESINDEX}=getStat(RES);
SERIESSTAT{SERIESINDEX}.fileName=SERIESSTAT{index}.fileName;
saveInfo(handles,SERIESSTAT{SERIESINDEX});
CD=get(handles.listbox1,'String');
[CDm,CDn]=size(CD);
CD(CDm+1,:)='КроссКор';
set(handles.listbox1,'String',CD);
set(handles.listbox1,'Value',SERIESINDEX);

if get(handles.checkbox1,'Value')==get(handles.checkbox1,'Max')
    figureTemp=figure;
    plot(RES(1,:));
end


% --------------------------------------------------------------------
function BuildColorMap_Callback(hObject, eventdata, handles)
% hObject    handle to BuildColorMap (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 SERIESNAMES
global SERIESINDEX
global SERIESTYPE
global SERIESSTAT

% Вычисляем матрицу кросс-кореляций для матрицы, выделенной в ListBox
index=get(handles.listbox1,'Value');
SRC=SERIES{index};
buildColorPlot(SRC);


% --------------------------------------------------------------------
function BuildEigenValues_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_1 (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 SERIESNAMES
global SERIESINDEX
global SERIESTYPE
global SERIESSTAT

% Вычисляем матрицу кросс-кореляций для матрицы, выделенной в ListBox
index=get(handles.listbox1,'Value');
SRC=SERIES{index};
[V,D]=jacobiHandle(SRC,0.001);
RES=[D V];

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

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

EigenValuesFigure(handles.figure1);


% --------------------------------------------------------------------
function ShuffleMatrix_Callback(hObject, eventdata, handles)
% hObject    handle to ShuffleMatrix (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 SERIESNAMES
global SERIESINDEX
global SERIESTYPE
global SERIESSTAT

% Вычисляем матрицу кросс-кореляций для матрицы, выделенной в ListBox
index=get(handles.listbox1,'Value');
SRC=SERIES{index};
RES=shuffleMatrix(SRC);

SERIESINDEX=SERIESINDEX+1;
SERIESNAMES{SERIESINDEX}=SERIESNAMES{index};
SERIES{SERIESINDEX}=RES;
SERIESTYPE{SERIESINDEX}=[char(SERIESTYPE{index}) ';SHM'];
SERIESSTAT{SERIESINDEX}=getStat(RES);
SERIESSTAT{SERIESINDEX}.fileName=SERIESSTAT{index}.fileName;
saveInfo(handles,SERIESSTAT{SERIESINDEX});
CD=get(handles.listbox1,'String');
[CDm,CDn]=size(CD);
CD(CDm+1,:)='ПеремешМ';
set(handles.listbox1,'String',CD);
set(handles.listbox1,'Value',SERIESINDEX);

if get(handles.checkbox1,'Value')==get(handles.checkbox1,'Max')
    figureTemp=figure;
    plot(RES(1,:));
end


% --------------------------------------------------------------------
function BuildPrm_Callback(hObject, eventdata, handles)
% hObject    handle to BuildPrm (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 SERIESNAMES
global SERIESINDEX
global SERIESTYPE
global SERIESSTAT

% Вычисляем матрицу кросс-кореляций для матрицы, выделенной в ListBox
index=get(handles.listbox1,'Value');
SRC=SERIES{index};
[m,n]=size(SRC);
[RESX,RESY]=buildPrm(m,n);

SERIESINDEX=SERIESINDEX+2;
SERIES{SERIESINDEX-1}=RESX;
SERIES{SERIESINDEX}  =RESY;
SERIESTYPE{SERIESINDEX-1}=[char(SERIESTYPE{index}) ';PrmX'];
SERIESTYPE{SERIESINDEX}  =[char(SERIESTYPE{index}) ';PrmY'];
SERIESSTAT{SERIESINDEX-1}=getStat(RESX);
SERIESSTAT{SERIESINDEX-1}.fileName=SERIESSTAT{index}.fileName;
SERIESSTAT{SERIESINDEX}  =getStat(RESY);
CD=get(handles.listbox1,'String');
[CDm,CDn]=size(CD);
CD(CDm+1,:)='Prm    X';
CD(CDm+2,:)='Prm    Y';
set(handles.listbox1,'String',CD);


% --------------------------------------------------------------------
function IPR_Callback(hObject, eventdata, handles)
% hObject    handle to IPR (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 SERIESNAMES
global SERIESINDEX
global SERIESTYPE
global SERIESSTAT

% Вычисляем матрицу кросс-кореляций для матрицы, выделенной в ListBox
index=get(handles.listbox1,'Value');
SRC=SERIES{index};
[m,n]=size(SRC);
if (abs(m-n)~=1)
    SRC2=[SRC(:,1) SRC(:,3:n)];
else
    SRC2=SRC;
end
[RESX,RESY]=IPRs(SRC2);

SERIESINDEX=SERIESINDEX+2;
SERIES{SERIESINDEX-1}=RESX;
SERIES{SERIESINDEX}  =RESY;
SERIESTYPE{SERIESINDEX-1}=[char(SERIESTYPE{index}) ';IprX'];
SERIESTYPE{SERIESINDEX}  =[char(SERIESTYPE{index}) ';IprY'];
SERIESSTAT{SERIESINDEX-1}=getStat(RESX);
SERIESSTAT{SERIESINDEX-1}.fileName=SERIESSTAT{index}.fileName;
SERIESSTAT{SERIESINDEX}  =getStat(RESY);
CD=get(handles.listbox1,'String');
[CDm,CDn]=size(CD);
CD(CDm+1,:)='Ipr    X';
CD(CDm+2,:)='Ipr    Y';
set(handles.listbox1,'String',CD);


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

MFDFATauFigure(handles.figure1);


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

% Розрахунок середнього значення матриці крос-кореляцій у вікні заданої
% ширини та при переміщенні вікна із заданим кроком

MeanCInWindowFigure(handles.figure1);


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

MaxLinksInWindowFigure(handles.figure1);


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

% Розрахунок власних значень матриці крос-кореляцій у вікні заданої
% ширини та при переміщенні вікна із заданим кроком

EigenValuesInWindowFigure(handles.figure1);


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


% --------------------------------------------------------------------
function MarkovChainsPredict_Callback(hObject, eventdata, handles)
% hObject    handle to MarkovChainsPredict (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 SERIESTYPE
global SERIESSTAT

index=get(handles.listbox1,'Value');
SRC=SERIES{index};
dlmwrite(strcat(tempdir,'Series.txt'),SRC,'\n');
global HHANDLES;
HHANDLES=handles;
global INFILENAME;
global OUTFILENAME;
INFILENAME=strcat(tempdir,'Series.txt');
OUTFILENAME=strcat(tempdir,'Series_MarkovPrognoz.txt');
MarkovChains;


% --------------------------------------------------------------------
function Sinustrend_prediction_Callback(hObject, eventdata, handles)
% hObject    handle to Sinustrend_prediction (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 SERIESTYPE
global SERIESSTAT

index=get(handles.listbox1,'Value');
SRC=SERIES{index};
dlmwrite(strcat(tempdir,'Series.txt'),SRC,'\n');
global HHANDLES;
HHANDLES=handles;

res_progn=sintrend_gui(strcat(tempdir,'Series.txt'),strcat(tempdir,'Series_SintrendRemains.txt'));


% --------------------------------------------------------------------
function LogSeries_Callback(hObject, eventdata, handles)
% Функция вычисление логарифма ряда
% hObject    handle to LogSeries (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 SERIESTYPE
global SERIESSTAT

index=get(handles.listbox1,'Value');
SRC=SERIES{index};
RES=log(SRC);

SERIESINDEX=SERIESINDEX+1;
SERIES{SERIESINDEX}=RES;
SERIESSTAT{SERIESINDEX}=getStat(RES);
SERIESSTAT{SERIESINDEX}.fileName=SERIESSTAT{index}.fileName;
SERIESTYPE{SERIESINDEX}=[char(SERIESTYPE{index}) ';ABS'];
saveInfo(handles,SERIESSTAT{SERIESINDEX});
CD=get(handles.listbox1,'String');
[CDm,CDn]=size(CD);
CD(CDm+1,:)='Логарифм';
set(handles.listbox1,'String',CD);
set(handles.listbox1,'Value',SERIESINDEX);

if get(handles.checkbox1,'Value')==get(handles.checkbox1,'Max')
    figure;
    plot(RES);
end



% --------------------------------------------------------------------
function Untitled_5_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_5 (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 SERIESTYPE
global SERIESSTAT

index=get(handles.listbox1,'Value');
SRC=SERIES{index};
dlmwrite(strcat(tempdir,'Series.txt'),SRC,'\n');
global HHANDLES;
HHANDLES=handles;

res_progn=sintrend_markov_gui(strcat(tempdir,'Series.txt'),strcat(tempdir,'Series_SintrendRemains.txt'));



% --------------------------------------------------------------------
function Exponent_series_Callback(hObject, eventdata, handles)
% Експонента - обернене перетворення від логарифму
% hObject    handle to Exponent_series (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 SERIESTYPE
global SERIESSTAT

index=get(handles.listbox1,'Value');
SRC=SERIES{index};
RES=exp(SRC);

SERIESINDEX=SERIESINDEX+1;
SERIES{SERIESINDEX}=RES;
SERIESSTAT{SERIESINDEX}=getStat(RES);
SERIESSTAT{SERIESINDEX}.fileName=SERIESSTAT{index}.fileName;
SERIESTYPE{SERIESINDEX}=[char(SERIESTYPE{index}) ';ABS'];
saveInfo(handles,SERIESSTAT{SERIESINDEX});
CD=get(handles.listbox1,'String');
[CDm,CDn]=size(CD);
CD(CDm+1,:)='Експон  ';
set(handles.listbox1,'String',CD);
set(handles.listbox1,'Value',SERIESINDEX);

if get(handles.checkbox1,'Value')==get(handles.checkbox1,'Max')
    figure;
    plot(RES);
end



% --------------------------------------------------------------------
function RecurrentGet_Callback(hObject, eventdata, handles)
% hObject    handle to RecurrentGet (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 SERIESTYPE
global SERIESSTAT

index=get(handles.listbox1,'Value');
SRC=SERIES{index};
[rr_pos,rr_neg]=recurr_intervals(SRC,1);
if (length(rr_pos)>0)
    RES=rr_pos;
    SERIESINDEX=SERIESINDEX+1;
    SERIES{SERIESINDEX}=RES;
    SERIESSTAT{SERIESINDEX}=getStat(RES);
    SERIESSTAT{SERIESINDEX}.fileName=SERIESSTAT{index}.fileName;
    SERIESTYPE{SERIESINDEX}=[char(SERIESTYPE{index}) ';ABS'];
    saveInfo(handles,SERIESSTAT{SERIESINDEX});
    CD=get(handles.listbox1,'String');
    [CDm,CDn]=size(CD);
    CD(CDm+1,:)='RR+     ';
    set(handles.listbox1,'String',CD);
    set(handles.listbox1,'Value',SERIESINDEX);
else
    fprintf(1,'Нету ни одного интервала.');
end
if( length(rr_neg)>0)
    RES=rr_neg;
    SERIESINDEX=SERIESINDEX+1;
    SERIES{SERIESINDEX}=RES;
    SERIESSTAT{SERIESINDEX}=getStat(RES);
    SERIESSTAT{SERIESINDEX}.fileName=SERIESSTAT{index}.fileName;
    SERIESTYPE{SERIESINDEX}=[char(SERIESTYPE{index}) ';ABS'];
    saveInfo(handles,SERIESSTAT{SERIESINDEX});
    CD=get(handles.listbox1,'String');
    [CDm,CDn]=size(CD);
    CD(CDm+1,:)='RR-     ';
    set(handles.listbox1,'String',CD);
    set(handles.listbox1,'Value',SERIESINDEX);
else
    fprintf(1,'Нету ни одного интервала.');
end

if get(handles.checkbox1,'Value')==get(handles.checkbox1,'Max')
    figure;
    plot(rr_pos);
    title('RR+');
    figure;
    plot(rr_neg);
    title('RR-');
end



% --------------------------------------------------------------------
function MovingWindowStats_Callback(hObject, eventdata, handles)
% hObject    handle to MovingWindowStats (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 SERIESTYPE
global SERIESSTAT

global HHANDLES;
HHANDLES=handles;

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


% --------------------------------------------------------------------
function MatrixStat_Callback(hObject, eventdata, handles)
% hObject    handle to MatrixStat (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 SERIESTYPE
global SERIESSTAT

global HHANDLES;
HHANDLES=handles;

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


% --------------------------------------------------------------------
function SeriesCut_Callback(hObject, eventdata, handles)
% hObject    handle to SeriesCut (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 SERIESTYPE
global SERIESSTAT

global HHANDLES;
HHANDLES=handles;

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

tsPartSeriesGet(SRC)


% --------------------------------------------------------------------
function Square_Callback(hObject, eventdata, handles)
% hObject    handle to Square (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 SERIESTYPE
global SERIESSTAT

global HHANDLES;
HHANDLES=handles;

index=get(handles.listbox1,'Value');
SRC=SERIES{index};
square_calc=SRC.*SRC;
RES=square_calc;
SERIESINDEX=SERIESINDEX+1;
SERIES{SERIESINDEX}=RES;
SERIESSTAT{SERIESINDEX}=getStat(RES);
SERIESSTAT{SERIESINDEX}.fileName=SERIESSTAT{index}.fileName;
SERIESTYPE{SERIESINDEX}=[char(SERIESTYPE{index}) ';Square'];
saveInfo(handles,SERIESSTAT{SERIESINDEX});
CD=get(handles.listbox1,'String');
[CDm,CDn]=size(CD);
CD(CDm+1,:)='Square  ';
set(handles.listbox1,'String',CD);
set(handles.listbox1,'Value',SERIESINDEX);

if get(handles.checkbox1,'Value')==get(handles.checkbox1,'Max')
    figure;
    plot(square_calc);
    title('Square');
end


% --------------------------------------------------------------------
function Geizenberg_Callback(hObject, eventdata, handles)
% hObject    handle to Geizenberg (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 SERIESTYPE
global SERIESSTAT

global HHANDLES;
HHANDLES=handles;

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

covarianceGUI(SRC);


% --------------------------------------------------------------------
function MarkovDefault_Callback(hObject, eventdata, handles)
% hObject    handle to MarkovDefault (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 SERIESTYPE
global SERIESSTAT

index=get(handles.listbox1,'Value');
SRC=SERIES{index};
dlmwrite(strcat(tempdir,'Series.txt'),SRC,'\n');
global HHANDLES;
HHANDLES=handles;
global INFILENAME;
global OUTFILENAME;
INFILENAME=strcat(tempdir,'Series.txt');
OUTFILENAME=strcat(tempdir,'Series_MarkovPrognoz.txt');
MarkovChainsCmd(INFILENAME,2500,OUTFILENAME);
Result=dlmread(OUTFILENAME);
Result=Result';
RES=Result;
SERIESINDEX=SERIESINDEX+1;
SERIES{SERIESINDEX}=RES;
SERIESSTAT{SERIESINDEX}=getStat(RES);
SERIESSTAT{SERIESINDEX}.fileName=SERIESSTAT{index}.fileName;
SERIESTYPE{SERIESINDEX}=[char(SERIESTYPE{index}) ';Markov'];
saveInfo(handles,SERIESSTAT{SERIESINDEX});
CD=get(handles.listbox1,'String');
[CDm,CDn]=size(CD);
CD(CDm+1,:)='МарковПр';
set(handles.listbox1,'String',CD);
set(handles.listbox1,'Value',SERIESINDEX);

if get(handles.checkbox1,'Value')==get(handles.checkbox1,'Max')
    figure;
    plot(Result);
    title('Square');
end


