📄 nnident.m
字号:
set(H.maxp_ptr,'userdata',maxp);
mint=get_param(arg1,'mint');
H.mint_ptr = uicontrol('Parent',fig,'visible','off');
set(H.mint_ptr,'userdata',mint);
maxt=get_param(arg1,'maxt');
H.maxt_ptr = uicontrol('Parent',fig,'visible','off');
set(H.maxt_ptr,'userdata',maxt);
Normalize=str2num(get_param(arg1,'Normalize'));
if isempty(Normalize) % If the field is empty we initialize default value.
Normalize=0;
end
H.Normalize_ptr = uicontrol('Parent',fig,'visible','off');
set(H.Normalize_ptr,'userdata',Normalize);
set(H.Normalize_data,'value',Normalize);
H.In_training_ptr = uicontrol('Parent',fig,'visible','off');
set(H.In_training_ptr,'userdata',0);
H.parent_function_ptr = uicontrol('Parent',fig,'visible','off');
set(H.parent_function_ptr,'userdata',arg3);
set(fig,'userdata',H);
set(H.error_messages,'string',sprintf('Generate or import data before training the neural network plant.'));
% IMPORTANT: This call must be here because we need H for the callback.
nncontrolutil('nnident','limit_output');
elseif strcmp(cmd,'limit_output') & (fig)
limit_output=get(H.Limit_output_data,'Value');
if limit_output==1
set(H.Max_output,'enable','on')
set(H.Max_output_text,'enable','on')
set(H.Min_output,'enable','on')
set(H.Min_output_text,'enable','on')
else
set(H.Max_output,'enable','off')
set(H.Max_output_text,'enable','off')
set(H.Min_output,'enable','off')
set(H.Min_output_text,'enable','off')
end
elseif strcmp(cmd,'data_no_ok') & (fig)
% set(H.Start_but,'enable','on')
set(H.Cancel_but,'enable','on')
if H.Training_done
set(H.OK_but,'enable','on')
set(H.Apply_but,'enable','on')
set(H.Handles.Menus.File.Save_NN,'enable','on')
set(H.Handles.Menus.File.Save_Exit_NN,'enable','on')
end
if H.Data_Available
load(cat(2,tempdir,'nnidentdata.mat'),'N2');
set(H.Start_but,'enable','on')
st=sprintf('Your training data set has %d samples.\nYou can now train the network.',N2-1);
set(H.error_messages,'string',st);
else
set(H.error_messages,'string',sprintf('Generate or import data before training the neural network plant.'));
set(H.Max_input,'enable','on')
set(H.Max_input_text,'enable','on')
set(H.Min_input,'enable','on')
set(H.Min_input_text,'enable','on')
set(H.max_int_edit,'enable','on')
set(H.max_int_text,'enable','on')
set(H.min_int_edit,'enable','on')
set(H.min_int_text,'enable','on')
set(H.Samples_text,'enable','on')
set(H.Samples,'enable','on')
set(H.Sampling_text,'enable','on')
set(H.Sampling_time,'enable','on')
set(H.Limit_output_data,'enable','on');
limit_output=get(H.Limit_output_data,'Value');
if limit_output==1
set(H.Max_output,'enable','on')
set(H.Max_output_text,'enable','on')
set(H.Min_output,'enable','on')
set(H.Min_output_text,'enable','on')
end
set(H.BrowseButton,'enable','on');
set(H.simulink_file,'enable','on');
set(H.simulink_file_text,'enable','on');
end
set(H.Simulating_text,'visible','off'); drawnow; % pause needed to refresh the message
fig2=findall(0,'type','figure','tag','nnidentdata');
delete(fig2);
if exist(cat(2,tempdir,'nnidentdata2.mat'))
delete(cat(2,tempdir,'nnidentdata2.mat'));
end
% We refresh the menu.
arg1=get(H.gcbh_ptr,'userdata');
arg2=get(H.gcb_ptr,'userdata');
nncontrolutil('nnident','',arg1,arg2,'');
elseif strcmp(cmd,'browsesim')
filterspec = '*.mdl';
udFileEdit = get(H.simulink_file,'UserData');
LastPath = udFileEdit.PathName;
CurrentPath=pwd;
if ~isempty(LastPath),
cd(LastPath);
end
[filename,pathname] = uigetfile(filterspec,'Simulink Plant Model:');
if ~isempty(LastPath),
cd(CurrentPath);
end
if filename,
if ~strcmpi(pathname(1:end-1),CurrentPath)
ImportStr = [pathname,filename(1:end-4)];
else
ImportStr = filename(1:end-4);
end
udFileEdit.PathName=pathname;
udFileEdit.FileName=filename;
set(H.simulink_file,'String',filename(1:end-4),'UserData',udFileEdit);
end
elseif strcmp(cmd,'clearpath') & (fig)
%---Callback for the SImulink File box
% Whenever a new name is entered, update the Userdata
NewName = get(gcbo,'String');
indDot = findstr(NewName,'.');
if ~isempty(indDot),
NewName=NewName(1:indDot(end)-1);
set(H.simulink_file,'String',NewName)
end
elseif strcmp(cmd,'erase_data') & (fig)
set(H.Max_input,'enable','on')
set(H.Max_input_text,'enable','on')
set(H.Min_input,'enable','on')
set(H.Min_input_text,'enable','on')
set(H.max_int_edit,'enable','on')
set(H.max_int_text,'enable','on')
set(H.min_int_edit,'enable','on')
set(H.min_int_text,'enable','on')
set(H.Samples_text,'enable','on')
set(H.Samples,'enable','on')
set(H.Sampling_text,'enable','on')
set(H.Sampling_time,'enable','on')
set(H.Limit_output_data,'enable','on');
limit_output=get(H.Limit_output_data,'Value');
if limit_output==1
set(H.Max_output,'enable','on')
set(H.Max_output_text,'enable','on')
set(H.Min_output,'enable','on')
set(H.Min_output_text,'enable','on')
end
set(H.BrowseButton,'enable','on');
set(H.simulink_file,'enable','on');
set(H.simulink_file_text,'enable','on');
H.Data_Generated=0;
H.Data_Imported=0;
H.Data_Available=0;
set(H.Start_but,'enable','off')
if exist(cat(2,tempdir,'nnidentdata2.mat'))
delete(cat(2,tempdir,'nnidentdata2.mat'));
end
if exist(cat(2,tempdir,'nnidentdata.mat'))
delete(cat(2,tempdir,'nnidentdata.mat'));
end
set(fig,'UserData',H);
set(H.Gen_data_but,'String','Generate Training Data', ...
'Callback','nncontrolutil(''nnident'',''gen_data'')', ...
'TooltipString','Generate data to be used in training the neural network plant model.');
set(H.error_messages,'string',sprintf('Generate or import data before training the neural network plant.'));
elseif (strcmp(cmd,'start_training') | strcmp(cmd,'continue_training') | strcmp(cmd,'data_ok') | ...
strcmp(cmd,'gen_data') | strcmp(cmd,'have_file')) & (fig)
if strcmp(cmd,'gen_data') & (fig)
% H.Data_Generated=1;
H.Data_Imported=0;
% H.Training_done=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.Start_but,'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.Sampling_time,'string'));
Ts=get_param(arg1,'Ts');
if length(a1) == 0,
present_error(fig,H,H.Sampling_time,Ts,1, ...
'You must initialize the sampling interval of your plant before training the neural network');
return
elseif a1<=0
present_error(fig,H,H.Sampling_time,Ts,1, ...
'You must set a positive sampling interval of your plant before training the neural network');
return
else Ts=a1; set(H.Ts_ptr,'userdata',Ts); end
fig2=findall(0,'type','figure','tag','nnidentdata');
if size(fig2,1)==0, fig2=0; end
if strcmp(cmd,'have_file')
if nargin==3 % Structure
if isempty(ImportStr) % Workspace
tr_dat=evalin('base',Data_Name);
if ~isfield(tr_dat,'.flag')
tr_dat.flag=ones(size(tr_dat.Y));
end
if ~isfield(tr_dat,'.Ts')
tr_dat.Ts=Ts;
end
else
a1 = ImportStr;
a2 = which(cat(2,a1,'.mat'));
if (length(a1) == 0 | length(a2) == 0),
present_error(fig,H,0,0,0, ...
'You must enter a valid filename for your training data, or the file directory must be defined in the MATLAB Path.');
return
else file_data=a1; end
temp=load (file_data,Data_Name);
tr_dat.U=getfield(temp,Data_Name,'U');
tr_dat.Y=getfield(temp,Data_Name,'Y');
if isfield(eval(cat(2,'temp.',Data_Name)),'flag')
tr_dat.flag=getfield(temp,Data_Name,'flag');
else
tr_dat.flag=ones(size(tr_dat.Y));
end
if isfield(eval(cat(2,'temp.',Data_Name)),'Ts')
tr_dat.Ts=getfield(temp,Data_Name,'Ts');
else
tr_dat.Ts=Ts;
end
end
else % Arrays.
if isempty(ImportStr) % Workspace
tr_dat=struct('U',evalin('base',U_Name),'Y',evalin('base',Y_Name));
tr_dat.flag=ones(size(tr_dat.Y));
tr_dat.Ts=Ts;
else
a1 = ImportStr;
a2 = which(cat(2,a1,'.mat'));
if (length(a1) == 0 | length(a2) == 0),
present_error(fig,H,0,0,0, ...
'You must enter a valid filename for your training data, or the file directory must be defined in the MATLAB Path.');
return
else file_data=a1; end
temp=load (file_data,U_Name,Y_Name);
tr_dat.U=getfield(temp,U_Name);
tr_dat.Y=getfield(temp,Y_Name);
tr_dat.flag=ones(size(tr_dat.Y));
tr_dat.Ts=Ts;
end
end
% We verify direction of the input vectors.
if size(tr_dat.U,1)<=1
tr_dat.U=tr_dat.U';
end
if size(tr_dat.Y,1)<=1
tr_dat.Y=tr_dat.Y';
end
if size(tr_dat.flag,1)<=1
tr_dat.flag=tr_dat.flag';
end
sam_training=size(tr_dat.Y,1)-1;
if fig2==0
pos_fig2=get(fig,'Position');
fig2 = figure('Units', H.StdUnit,...
'CloseRequestFcn','nncontrolutil(''nnident'',''data_NO_ok'');', ...
'Interruptible','off', ...
'BusyAction','cancel', ...
'HandleVis','Callback', ...
'Name', 'Plant Input-Output Data',...
'Tag', 'nnidentdata',...
'NumberTitle', 'off',...
'Position', pos_fig2, ...
'IntegerHandle', 'off',...
'Toolbar', 'none', ...
'WindowStyle','modal');
f2.h1=axes('Position',[0.13 0.60 0.74 0.32],'Parent',fig2);
f2.h2=axes('Position',[0.13 0.15 0.74 0.32],'Parent',fig2);
f2.message= uicontrol('Parent',fig2, ...
'Units',H.StdUnit, ...
'BackgroundColor',[0.752941176470588 0.752941176470588 0.752941176470588], ...
'FontWeight','bold', ...
'ForegroundColor',[0 0 1], ...
'ListboxTop',0, ...
'Position',[156 3 188 20], ...
'Style','text', ...
'Tag','StaticText1');
else
f2=get(fig2,'userdata');
figure(fig2);
end
f2.accept_but = uicontrol('Parent',fig2, ...
'Units',H.StdUnit, ...
'BackgroundColor',[0.752941176470588 0.752941176470588 0.752941176470588], ...
'Callback','nncontrolutil(''nnident'',''data_ok'');', ...
'ListboxTop',0, ...
'Position',[2 2 68.75 15], ...
'String','Accept Data', ...
'Tag','Pushbutton1');
st=sprintf('The imported data has %d samples.\nPlease Accept or Reject Data to continue.',sam_training);
set(H.error_messages,'string',st);
set(f2.message,'string',st);
else %strcmp(cmd,'gen_data')
a1 = get(H.simulink_file,'string');
udFileEdit = get(H.simulink_file,'UserData');
LastPath = udFileEdit.PathName;
if isempty(LastPath),
a2 = which(cat(2,a1,'.mdl'));
else
a2 = which(cat(2,LastPath,cat(2,a1,'.mdl')));
end
if (length(a1) == 0 | length(a2) == 0),
present_error(fig,H,H.simulink_file,a1,0, ...
'You must enter a valid filename for your Simulink plant model');
return
else
sim_file=a1;
OpenFlag=1;
ErrorFlag=isempty(find_system(0,'flat','Name',sim_file));
if ErrorFlag,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -