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

% Last Modified by GUIDE v2.5 12-Oct-2008 16:44:33

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

% Choose default command line output for generate
handles.output = hObject;
handles.handlesmain=varargin{1};
% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = generate_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 edit_smin_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_smin (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 edit_smin_Callback(hObject, eventdata, handles)
% hObject    handle to edit_smin (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 edit_smin as text
%        str2double(get(hObject,'String')) returns contents of edit_smin as a double


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


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


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


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


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


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


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


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


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

global hierarchy_params;
global in_series;
y=in_series;
hierarchy_params=[];
nbeg=str2num(get(handles.handlesmain.edit3,'String'));
nend=str2num(get(handles.handlesmain.edit4,'String'));
N=nend-nbeg;

% Определяем наиболее оптимальную длину ряда при заданной иерархии.
type_hier=get(handles.handlesmain.radiobutton9,'Value');
if (type_hier==0)
    [N1,masm,masdt]=getbestlength(N,[2,3,5,7,11,13,17,19,23,29]);
    %Возможно использование простой иерархии - степени двоек.
else
    [N1,masdt]=getstepen2(N);
end
% Обрезаем входной ряд, убирая лишние начальные точки.
nbeg=nend-N1;
set(handles.handlesmain.edit3,'String',num2str(nbeg-1));
y_urez=y(nbeg:nend);%(N-N1:N);
%if size(y_urez,1)>size(y_urez,2)
%    y_urez=transpose(y_urez);
%end
% блок прогнозирования для каждого варианта приращения.
rewmasdt=-sort(-masdt);
%rewmasdt=rewmasdt%(3:length(rewmasdt));
masdtparam=[];
% 252-3,210-8, 180-10,140-2,126-13, 105-15,90-2,84-13,70-5,63-2, 60-11,45-11 - сп500
%sin30 :168-14,140-10,120-2,105-8,84-6,70-9,60-5,56-9,42-3,40-3,35-4,30-4, 28-9,24-15,21-4,20-3,15-7,14-15,12-7,10-2,8-10,7-9,6-8,5-6,4-4,3-9,2-5
masiv_s=[2,2,2,2,14,10,2,8,6,9,5,9,3,3,4,4,9,5,4,3,7,15,7,2,10,9,8,6,4,9,5];
masiv_k=[2,2,2];
%dtmanual=length(masiv_s);
dtmanual=length(rewmasdt);
str=cell(dtmanual-2,1);
%fprintf(f,'function temporary001(dt);\n');
f=fopen('temporary001.m','w');
fprintf(f,'function dtparam=temporary001(dt);\ndtparam=[];\n');
fprintf(f,'dtparam.smin=%s;\n',get(handles.edit_smin,'String'));
fprintf(f,'dtparam.smax=%s;\n',get(handles.edit_smax,'String'));
fprintf(f,'dtparam.s=%s;\n',get(handles.edit_s,'String'));
fprintf(f,'dtparam.rmin=%s;\n',get(handles.edit_rmin,'String'));
fprintf(f,'rmax=%s;\n',get(handles.edit_rmax,'String'));
fprintf(f,'r=%s;\n',get(handles.edit_r,'String'));
fprintf(f,'kmin=%s;\n',get(handles.edit_kmin,'String'));
fprintf(f,'kmax=%s;\n',get(handles.edit_kmax,'String'));
fprintf(f,'k=%s;\n',get(handles.edit_k,'String'));
fprintf(f,'tbeg=%s;\n',get(handles.edit_tbeg,'String'));
fprintf(f,'tend=%s;\n',get(handles.edit_tend,'String'));
fprintf(f,'tbeg_sost=%s;\n',get(handles.edit_tbeg_sost,'String'));
fprintf(f,'tend_sost=%s;\n',get(handles.edit_tend_sost,'String'));

fclose(f);
tests=get(handles.checkbox_tests,'Value');
testk=get(handles.checkbox_testk,'Value');
testr=get(handles.checkbox_testr,'Value');
for numdt=3:dtmanual
    dtparam=[];
    dtparam.dt=rewmasdt(numdt);
    %[smin,smax,s,rmin,rmax,r,kmin,kmax,k,tbeg,tend,tbeg_sost,tend_sost]=temporary001(dtparam.dt);
    dtparam==temporary001(dtparam.dt);
    dtparam.r=r; %Нужно выражение как-то. Зависимость от дт.
    dtparam.rmin=rmin;
    dtparam.rmax=rmax;
    dtparam.testr=testr;
    dtparam.s=s;%+round(sqrt(numdt));%masiv_s(numdt);
    dtparam.smin=smin;
    dtparam.smax=smax;
    dtparam.tests=tests;
    dtparam.k=k;
    dtparam.kmin=kmin;
    dtparam.kmax=kmax;
    dtparam.testk=testk;
    dtparam.tbeg=tbeg;
    dtparam.tend=tend;
    dtparam.tbeg_sost=tber_sost;
    dtparam.tend_sost=tend_sost;
    dtparam.use=1;
    str(dtmanual-numdt+1)={num2str(rewmasdt(numdt))};
    masdtparam=[dtparam,masdtparam];
end
set(handles.handlesmain.popupmenu1,'String',str);
hierarchy_params=masdtparam;
global in_series_urez;
in_series_urez=y_urez;
close(handles.figure1);


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


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


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


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


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


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


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


