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

% Last Modified by GUIDE v2.5 11-Sep-2009 09:20:42

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

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

handles.baseFigure=varargin{1};

handles.plotFigure=figure;

% Update handles structure
guidata(hObject, handles);


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

%========================================================================================================================
% --- Outputs from this function are returned to the command line.
function varargout = MFDFAFigure_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 pushbuttonClose.
function pushbuttonClose_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonClose (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

delete(handles.plotFigure);
delete(handles.figure1);

%========================================================================================================================
% --- Executes on button press in pushbuttonGo.
function pushbuttonGo_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonGo (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 TEMPX
global TEMPY

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

POWERS=get(handles.edit1,'String');
index=1;
POWERS_num=0;
number='0';
[m,n]=size(POWERS);
for i=1:n
    if POWERS(1,i)~=';'
        number=[number POWERS(1,i)];
    else
        POWERS_num(index)=str2num(number);
        number='0';
        index=index+1;
    end
end
if mystrcmp(number,'0')~=0
    POWERS_num(index)=str2num(number);
end

[m,n]=size(POWERS_num);
if get(handles.checkbox3,'Value')==get(handles.checkbox3,'Min')
    for i=n:-1:1
        POWERS_num_minus(n-i+1)=-POWERS_num(i);
    end
end

COLORS='bgrcmyk';
[m,n]=size(POWERS_num);

if get(handles.checkbox3,'Value')==get(handles.checkbox3,'Min')
    stepEnd=2*n;
else
    stepEnd=n;
end
addToProgress(0,1,stepEnd+1);

index=1;
if get(handles.checkbox3,'Value')==get(handles.checkbox3,'Min')
    for i=1:n
        [TX,TY]=multiDFAe(SRC,10,300,POWERS_num_minus(i),50,1.1);
        addToProgress(i);
        if get(handles.checkboxLogX,'Value')==get(handles.checkboxLogX,'Max')
            XNew=log(TX);
        else
            XNew=TX;
        end
        if get(handles.checkboxLogY,'Value')==get(handles.checkboxLogY,'Max')
            YNew=log(TY);
        else
            YNew=TY;
        end
        [a,b]=MinSquare(XNew,YNew);
%        disp(POWERS_num_minus(i));
        X(i,:)=TX;
        Y(i,:)=TY;
        LEGEND(i,:)=blanks_w2(['q=' num2str(POWERS_num_minus(i)) '; \alpha=' num2str(a)],25);
    end
    index=n+1;
end
for i=1:n
    [TX,TY]=multiDFAe(SRC,10,300,POWERS_num(i),50,1.1);
    addToProgress(stepEnd-n+i);
    if get(handles.checkboxLogX,'Value')==get(handles.checkboxLogX,'Max')
        XNew=log(TX);
    else
        XNew=TX;
    end
    if get(handles.checkboxLogY,'Value')==get(handles.checkboxLogY,'Max')
        YNew=log(TY);
    else
        YNew=TY;
    end
    [a,b]=MinSquare(XNew,YNew);
%    disp(POWERS_num(i));
    X(index,:)=TX;
    Y(index,:)=TY;
    LEGEND(index,:)=blanks_w2(['q=' num2str(POWERS_num(i)) '; \alpha=' num2str(a)],25);
    index=index+1;
end
addToProgress(stepEnd+1);

figure(handles.plotFigure);

loglog(X(1,:),Y(1,:),[COLORS(1) '*-']);
hold on;
for i=2:n
    loglog(X(i,:),Y(i,:),[COLORS(i) '*-']);
end
for i=n+1:2*n
    loglog(X(i,:),Y(i,:),[COLORS(2*n-i+1) '.-']);
end

legend(LEGEND);
hold off

figure(handles.figure1);

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

if get(hObject,'Value')==get(hObject,'Max')
    set(get(handles.plotFigure,'CurrentAxes'),'YScale','log');
else
    set(get(handles.plotFigure,'CurrentAxes'),'YScale','linear');
end

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

if get(hObject,'Value')==get(hObject,'Max')
    set(get(handles.plotFigure,'CurrentAxes'),'XScale','log');
else
    set(get(handles.plotFigure,'CurrentAxes'),'XScale','linear');
end

% --- 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


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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 edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (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 edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- 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 SERIES
global SERIESINDEX
global SERIESTYPE
global SERIESSTAT

ST=SERIESSTAT{get(handles.listbox1,'Value')}.fileName;
[mST,nST]=size(ST);

if get(handles.checkbox4,'Value')==get(handles.checkbox4,'Max')
    set(handles.edit1,'String',[ST(1,1:nST-3) 'DT']);
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


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (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 edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (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 edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (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 edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (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 edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


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

LINES=get(get(handles.plotFigure,'CurrentAxes'),'Children');
[LINESm,LINESn]=size(LINES);
%if LINESm>1
%    for i=1:LINESm-1
%        delete(LINES(i,1))
%    end
%end
X=get(LINES(LINESm,1),'XData');
Y=get(LINES(LINESm,1),'YData');
if get(handles.checkboxLogX,'Value')==get(handles.checkboxLogX,'Max')
    XNew=log(X);
else
    XNew=X;
end
if get(handles.checkboxLogY,'Value')==get(handles.checkboxLogY,'Max')
    YNew=log(Y);
else
    YNew=Y;
end

TEMP=get(handles.pushbuttonMinSquare,'String');
set(handles.pushbuttonMinSquare,'String','Выберите на графике 2 точки.');
figure(handles.plotFigure);
[mouseX,mouseY]=ginput(2);
figure(handles.figure1);
set(handles.pushbuttonMinSquare,'String',TEMP);

[Xm,Xn]=size(X);
Min=abs(X(1,1)-mouseX(1,1));
indexMin1=1;
for i=2:Xn
    if abs(X(1,i)-mouseX(1,1))<Min
        Min=abs(X(1,i)-mouseX(1,1));
        indexMin1=i;
    end
end

Min=abs(X(1,1)-mouseX(2,1));
indexMin2=1;
for i=2:Xn
    if abs(X(1,i)-mouseX(2,1))<Min
        Min=abs(X(1,i)-mouseX(2,1));
        indexMin2=i;
    end
end

if indexMin1>indexMin2
    TEMP=indexMin1;
    indexMin1=indexMin2;
    indexMin2=TEMP;
end
[a,b]=MinSquare(XNew(1,indexMin1:indexMin2),YNew(1,indexMin1:indexMin2));
set(handles.text4,'String',num2str(a));
set(handles.text5,'String',num2str(b));
% Осталось из старой версии
%plot([X(1,indexMin1) X(1,indexMin2)],[Y(1,indexMin1) Y(1,indexMin2)],'r.-');

XL=get(get(handles.plotFigure,'CurrentAxes'),'XLim');
YL=get(get(handles.plotFigure,'CurrentAxes'),'YLim');

YNew2=a*XNew+b;
if get(handles.checkboxLogY,'Value')==get(handles.checkboxLogY,'Max')
    YNew2=exp(YNew2);
end

figure(handles.plotFigure);
hold on;
plot(X,YNew2,'r-');
hold off;
figure(handles.figure1);
set(get(handles.plotFigure,'CurrentAxes'),'XLim',XL);
set(get(handles.plotFigure,'CurrentAxes'),'YLim',YL);


% --- Executes during object creation, after setting all properties.
function editOX_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editOX (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 editOX_Callback(hObject, eventdata, handles)
% hObject    handle to editOX (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 editOX as text
%        str2double(get(hObject,'String')) returns contents of editOX as a double


% --- Executes during object creation, after setting all properties.
function editOY_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editOY (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 editOY_Callback(hObject, eventdata, handles)
% hObject    handle to editOY (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 editOY as text
%        str2double(get(hObject,'String')) returns contents of editOY as a double


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

LabelX=get(get(handles.plotFigure,'CurrentAxes'),'XLabel');
LabelY=get(get(handles.plotFigure,'CurrentAxes'),'YLabel');
set(LabelX,'String',get(handles.editOX,'String'));
set(LabelX,'FontWeight','bold');
set(LabelX,'FontSize',str2num(get(handles.editFontSize,'String')));
set(LabelY,'String',get(handles.editOY,'String'));
set(LabelY,'FontWeight','bold');
set(LabelY,'FontSize',str2num(get(handles.editFontSize,'String')));


% --- Executes during object creation, after setting all properties.
function editFontSize_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editFontSize (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 editFontSize_Callback(hObject, eventdata, handles)
% hObject    handle to editFontSize (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 editFontSize as text
%        str2double(get(hObject,'String')) returns contents of editFontSize as a double


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

NAME=char(SERIESTYPE{get(handles.listbox1,'Value')});
[mT,nT]=size(NAME);
if (NAME(1,nT)=='X')
    X=SERIES{get(handles.listbox1,'Value')};
    Y=SERIES{get(handles.listbox1,'Value')+1};
elseif (NAME(1,nT)=='Y')
    X=SERIES{get(handles.listbox1,'Value')-1};
    Y=SERIES{get(handles.listbox1,'Value')};
else
    Y=SERIES{get(handles.listbox1,'Value')};
    [m,n]=size(Y);
    X=[1:n];
end

figure(handles.plotFigure);

hold on;
if (get(handles.checkboxLogX,'Value')==get(handles.checkboxLogX,'Max'))
    if (get(handles.checkboxLogY,'Value')==get(handles.checkboxLogY,'Max'))
        loglog(X,Y,'y');
    else
        semilogx(X,Y,'y');
    end
else
    if (get(handles.checkboxLogY,'Value')==get(handles.checkboxLogY,'Max'))
        semilogy(X,Y,'y');
    else
        plot(X,Y,'y');
    end
end
hold off;

figure(handles.figure1);


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

global TEMPX
global TEMPY

global CHLD

SIGMA=1;
if get(handles.checkboxAddGauss,'Value')==get(handles.checkboxAddGauss,'Max')
%%%    
    CHLD=get(get(handles.plotFigure,'CurrentAxes'),'Children');
    [m,n]=size(CHLD);
    if (get(handles.checkboxLogY,'Value')==get(handles.checkboxLogY,'Max'))
       RES1=minAfter(get(CHLD(1,1),'YData'),0);
    else
       RES1=minAfter(get(CHLD(1,1),'YData'),-1);
    end
    for i=2:m
        if (get(handles.checkboxLogY,'Value')==get(handles.checkboxLogY,'Max'))
           RES2=minAfter(get(CHLD(1,1),'YData'),0);
        else
           RES2=minAfter(get(CHLD(1,1),'YData'),-1);
        end
        if RES2<RES1
            RES1=RES2;
        end
    end
%%%
    figure(handles.plotFigure);
    hold on
    RESY=Gauss(TEMPX,SIGMA,0);
    [m,n]=size(TEMPX);
    index=1;
    X=0;
    Y=0;
    for i=1:n
        if (TEMPX(1,i)>=-3*SIGMA) && (TEMPX(1,i)<=3*SIGMA) && (RESY(1,i)>=RES1) %%%
           X(1,index)=TEMPX(1,i);
           Y(1,index)=RESY(1,i);
           index=index+1;
       end
    end
    
    
    if (get(handles.checkboxLogX,'Value')==get(handles.checkboxLogX,'Max'))
        if (get(handles.checkboxLogY,'Value')==get(handles.checkboxLogY,'Max'))
            loglog(X,Y,'-r');
        else
            semilogx(X,Y,'-r');
        end
    else
        if (get(handles.checkboxLogY,'Value')==get(handles.checkboxLogY,'Max'))
            semilogy(X,Y,'-r');
        else
            plot(X,Y,'-r');
        end
    end
    CHLD=get(get(handles.plotFigure,'CurrentAxes'),'Children');
    CHLD=CHLD(1,1);
    hold off
    figure(handles.figure1);
else
    if CHLD~=0
        delete(CHLD);
        CHLD=0;
    end
end


% --- Executes during object creation, after setting all properties.
function edit8_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editOY (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 edit8_Callback(hObject, eventdata, handles)
% hObject    handle to editOY (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 editOY as text
%        str2double(get(hObject,'String')) returns contents of editOY as a double


% --- Executes on button press in pushbuttonRefresh.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonRefresh (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 edit9_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editFontSize (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 edit9_Callback(hObject, eventdata, handles)
% hObject    handle to editFontSize (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 editFontSize as text
%        str2double(get(hObject,'String')) returns contents of editFontSize as a double
