function nnmodref(cmd,arg1,arg2,arg3)
%NNMODREF Neural Network Model Reference Controller GUI for Neural Network Controller Toolbox.
%
% Synopsis
%
% nnmodref(cmd,arg1,arg2,arg3)
%
% Warning!!
%
% This function may be altered or removed in future
% releases of the Neural Network Toolbox. We recommend
% you do not write code which calls this function.
% This function is generally being called from a Simulink block.
% Orlando De Jesus, Martin Hagan, 1-25-00
% Copyright 1992-2004 The MathWorks, Inc.
% $Revision: 1.7.2.1 $ $Date: 2004/03/24 20:42:49 $
% CONSTANTS
me = 'Model Reference Control';
% DEFAULTS
if nargin == 0, cmd = ''; else cmd = lower(cmd); end
% FIND WINDOW IF IT EXISTS
fig = 0;
% 9/3/99 We alow the program to see hidden handles
fig=findall(0,'type','figure','name',me);
if (size(fig,1)==0), fig=0; end
if length(get(fig,'children')) == 0, fig = 0; end
% GET WINDOW DATA IF IT EXISTS
if fig
H = get(fig,'userdata');
if strcmp(cmd,'')
if get(H.gcbh_ptr,'userdata')~=arg1
delete(fig);
fig=0;
end
else
if strcmp(cmd,'close')
delete(fig)
return;
end
% ODJ 1-13-00 We check if the field SimulationStatus exist before reading that field
if isfield(get(H.gcbh_ptr,'userdata'))
if isfield(get_param(get_param(get(H.gcbh_ptr,'userdata'),'parent'),'objectparameters'),'SimulationStatus')
SimulationStatus=get_param(get_param(get(H.gcbh_ptr,'userdata'),'parent'),'simulationstatus');
else
SimulationStatus='none';
end
else
SimulationStatus='none';
end
if (strcmp(SimulationStatus,'running') | strcmp(SimulationStatus,'paused')) & ~strcmp(cmd,'close')
set(H.error_messages,'string','You must stop the simulation to change NN configuration parameters.');
return;
end
end
end
%==================================================================
% Activate the window.
%
% ME() or ME('')
%==================================================================
if strcmp(cmd,'')
if fig
figure(fig)
if strcmp(arg3,'nnident')
set(H.error_messages,'string',sprintf('Generate or import data before training the neural network controller.'));
else
set(H.error_messages,'string',sprintf('Perform plant identification before controller training.'));
end
set(fig,'visible','on')
else
nncontrolutil('nnmodref','init',arg1,arg2,arg3)
end
%==================================================================
% Close the window.
%
% ME() or ME('')
%==================================================================
elseif strcmp(cmd,'close') & (fig)
delete(fig)
return;
elseif strcmp(cmd,'stop_sim')
fig2=findall(0,'type','figure','tag','ind_adap_data');
if (size(fig2,1)==0), fig2=0; end
f2=get(fig2,'userdata');
f2.stop=1;
set(fig2,'UserData',f2);
return;
elseif (strcmp(cmd,'apply') | strcmp(cmd,'ok')) & (fig)
arg1=get(H.gcbh_ptr,'userdata');
epochs_c = get(H.epochs_c_ptr,'userdata');
set_param(arg1,'epochs_c',num2str(epochs_c));
retraining_c = get(H.retraining_c_ptr,'userdata');
set_param(arg1,'retraining_c',num2str(retraining_c));
max_r = get(H.max_r_ptr,'userdata');
set_param(arg1,'max_r',num2str(max_r));
min_r = get(H.min_r_ptr,'userdata');
set_param(arg1,'min_r',num2str(min_r));
max_r_int = get(H.max_r_int_ptr,'userdata');
set_param(arg1,'max_r_int',num2str(max_r_int));
min_r_int = get(H.min_r_int_ptr,'userdata');
set_param(arg1,'min_r_int',num2str(min_r_int));
sam_training_c = get(H.sam_training_c_ptr,'userdata');
set_param(arg1,'sam_training_c',num2str(sam_training_c));
S1c = get(H.S1c_ptr,'userdata');
set_param(arg1,'S1c',num2str(S1c));
Ref_file = get(H.Ref_file_ptr,'userdata');
set_param(arg1,'Ref_file',Ref_file);
Nrc = get(H.Nrc_ptr,'userdata');
set_param(arg1,'Nrc',num2str(Nrc));
Nic = get(H.Nic_ptr,'userdata');
set_param(arg1,'Nic',num2str(Nic));
Njc = get(H.Njc_ptr,'userdata');
set_param(arg1,'Njc',num2str(Njc));
Use_Inc_training = get(H.Use_Inc_training_ptr,'userdata');
set_param(arg1,'Use_Inc_training',num2str(Use_Inc_training));
Use_Previous_Weights = get(H.Use_Previous_Weights_ptr,'userdata');
set_param(arg1,'Use_Previous_Weights',num2str(Use_Previous_Weights));
IW_r = get(H.IW_r_ptr,'userdata');
IW_u = get(H.IW_u_ptr,'userdata');
IW_y = get(H.IW_y_ptr,'userdata');
LW_c = get(H.LW_c_ptr,'userdata');
B1_c = get(H.B1_c_ptr,'userdata');
B2_c = get(H.B2_c_ptr,'userdata');
set_param(arg1,'IW_y',mat2str(IW_y,20));
set_param(arg1,'IW_u',mat2str(IW_u,20));
set_param(arg1,'IW_r',mat2str(IW_r,20));
set_param(arg1,'LW_c',mat2str(LW_c,20));
set_param(arg1,'B1_c',mat2str(B1_c,20));
set_param(arg1,'B2_c',mat2str(B2_c,20));
if strcmp(cmd,'ok')
delete(fig)
end
%==================================================================
% Execute Identification Training.
%
% ME('training')
%==================================================================
elseif strcmp(cmd,'training') & (fig)
arg1=get(H.gcbh_ptr,'userdata');
arg2=get(H.gcb_ptr,'userdata');
nnident('',arg1,arg2,'nnmodref');
%==================================================================
% Execute Controller Training.
%
% ME('training')
%==================================================================
elseif (strcmp(cmd,'training_con') | strcmp(cmd,'cont_training_con') | strcmp(cmd,'data_ok') | ...
strcmp(cmd,'gen_data') | strcmp(cmd,'have_file')) & (fig)
if strcmp(cmd,'gen_data') & (fig)
H.Data_Imported=0;
set(fig,'UserData',H);
elseif strcmp(cmd,'have_file') & (fig)
ImportStr=arg1;
H.Data_Imported=1;
if nargin==3
Data_Name=arg2;
else
U_Name=arg2;
Y_Name=arg3;
end
end
set(H.Train_con,'enable','off')
set(H.Cancel_but,'enable','off')
set(H.OK_but,'enable','off')
set(H.Apply_but,'enable','off')
set(H.Handles.Menus.File.Save_NN,'enable','off')
set(H.Handles.Menus.File.Save_Exit_NN,'enable','off')
if (strcmp(cmd,'gen_data') | strcmp(cmd,'have_file'))%strcmp(cmd,'start_training')
arg1=get(H.gcbh_ptr,'userdata');
a1 = str2num(get(H.max_r_edit,'string'));
if length(a1) == 0,
max_r=get_param(arg1,'max_r');
present_error(fig,H,H.max_r_edit,max_r,1, ...
'Please correct the maximum reference value');
return
else max_r=a1; set(H.max_r_ptr,'userdata',a1); end
a1 = str2num(get(H.min_r_edit,'string'));
min_r=get_param(arg1,'min_r');
if length(a1) == 0,
present_error(fig,H,H.min_r_edit,min_r,1, ...
'Please correct the minimum reference value.');
return
elseif a1>=max_r
present_error(fig,H,H.min_r_edit,min_r,1, ...
'Please correct the maximum and minimum reference values.');
return
else min_r=a1; set(H.min_r_ptr,'userdata',a1); end
a1 = str2num(get(H.max_r_int_edit,'string'));
if (length(a1) == 0) | a1<=0,
max_r_int=get_param(arg1,'max_r_int');
present_error(fig,H,H.max_r_int_edit,max_r_int,1, ...
'You must enter a valid number for the maximum interval value over which the random input is constant.');
return
else max_r_int=a1; set(H.max_r_int_ptr,'userdata',a1); end
a1 = str2num(get(H.min_r_int_edit,'string'));
min_r_int=get_param(arg1,'min_r_int');
if (length(a1) == 0) | a1<=0,
present_error(fig,H,H.mi