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 23-Dec-2008 17:51:01

% 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 structures
N=str2num(get(handles.handlesmain.edit4,'String'))-str2num(get(handles.handlesmain.edit3,'String'));
s=str2num(get(handles.handlesmain.edit7,'String'));
k=str2num(get(handles.handlesmain.edit9,'String'));
set(handles.edit_tend,'String',num2str(N-1));
set(handles.edit_tend_sost,'String',num2str(N-1));
handles.s=s;
handles.k=k;
set(handles.edit_s,'String',num2str(s));
set(handles.edit_smax,'String',num2str(s+3));
set(handles.edit_k,'String',num2str(k));
set(handles.edit_kmax,'String',num2str(k+2));
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_rminmin_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_rminmin (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_rminmin_Callback(hObject, eventdata, handles)
% hObject    handle to edit_rminmin (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_rminmin as text
%        str2double(get(hObject,'String')) returns contents of edit_rminmin as a double


% --- Executes during object creation, after setting all properties.
function edit_rminmax_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_rminmax (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_rminmax_Callback(hObject, eventdata, handles)
% hObject    handle to edit_rminmax (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_rminmax as text
%        str2double(get(hObject,'String')) returns contents of edit_rminmax 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_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');
nnn=round(2*N/3);
if (type_hier==0)
    [N1,masm,masdt]=getbestlength(nnn,[2,3,5,7,11,13,17,19,23,29]);
    %Возможно использование простой иерархии - степени двоек.
else
    [N1,masdt]=getstepen2(nnn);
end
% Обрезаем входной ряд, убирая лишние начальные точки.
nbeg=nend-3*N1/2;
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.dt=dt;\n',get(handles.edit_smin,'String'));
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.rminmin=%s;\n',get(handles.edit_rminmin,'String'));
fprintf(f,'dtparam.rminmax=%s;\n',get(handles.edit_rminmax,'String'));
fprintf(f,'dtparam.rmin=%s;\n',get(handles.edit_rmin,'String'));
fprintf(f,'dtparam.rmaxmin=%s;\n',get(handles.edit_rmaxmin,'String'));
fprintf(f,'dtparam.rmaxmax=%s;\n',get(handles.edit_rmaxmax,'String'));
fprintf(f,'dtparam.rmax=%s;\n',get(handles.edit_rmax,'String'));
fprintf(f,'dtparam.kmin=%s;\n',get(handles.edit_kmin,'String'));
fprintf(f,'dtparam.kmax=%s;\n',get(handles.edit_kmax,'String'));
fprintf(f,'dtparam.k=%s;\n',get(handles.edit_k,'String'));
fprintf(f,'dtparam.tbeg=%s;\n',get(handles.edit_tbeg,'String'));
fprintf(f,'dtparam.tend=%s;\n',get(handles.edit_tend,'String'));
fprintf(f,'dtparam.tbeg_sost=%s;\n',get(handles.edit_tbeg_sost,'String'));
fprintf(f,'dtparam.tend_sost=%s;\n',get(handles.edit_tend_sost,'String'));
fprintf(f,'dtparam.tests=%d;\n',get(handles.checkbox_tests,'Value'));
fprintf(f,'dtparam.testrmin=%d;\n',get(handles.checkbox_testrmin,'Value'));
fprintf(f,'dtparam.testrmax=%d;\n',get(handles.checkbox_testrmax,'Value'));
fprintf(f,'dtparam.testk=%d;\n',get(handles.checkbox_testk,'Value'));

fclose(f);
tests=get(handles.checkbox_tests,'Value');
testk=get(handles.checkbox_testk,'Value');
testr=get(handles.checkbox_testrmin,'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.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_testrmin.
function checkbox_testrmin_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_testrmin (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_testrmin


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


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


% --- Executes during object creation, after setting all properties.
function edit_rmaxmax_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_rmaxmax (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_rmaxmax_Callback(hObject, eventdata, handles)
% hObject    handle to edit_rmaxmax (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_rmaxmax as text
%        str2double(get(hObject,'String')) returns contents of edit_rmaxmax 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 on button press in checkbox_testrmax.
function checkbox_testrmax_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_testrmax (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_testrmax


