📄 quademo.m
字号:
quademo('reset',hfigure);
else
errordlg('This file does not contain required data.','Bad file','modal');
end
end
case 'redraw'
% == Redraw points in axes ======================================================
h=get(hfigure,'UserData'); % uicontrol handlers
% get sets with points
sets=get(h.axes1,'UserData');
if isempty(sets)==1,
return;
end
% clears axes
set(get(h.axes1,'Children'),'EraseMode','normal');
clrchild(h.axes1);
h.line.handler1=plot(...
'Parent',h.axes1,...
[0],[0],'Visible','off','LineWidth',LINE_WIDTH,'EraseMode','xor','Color','k');
h.line.handler2=plot(...
'Parent',h.axes1,...
[0],[0],'Visible','off','LineWidth',LINE_WIDTH,'EraseMode','xor','Color','k');
set(hfigure,'UserData',h); % uicontrol handlers
quademo('drawline',hfigure,h.line.fi,h.line.alpha);
% Appear point in 2D plane.
%% pplot(sets.X,sets.I);
ppoints(sets.X,sets.I);
drawnow;
case 'loadsets'
% == Load sets ==================================================================
% Get file name from the pop up menu according to menu pointer. Than clear axes,
% load new file and appear the points from him.
h=get(hfigure,'UserData'); % uicontrol handlers
% Clear axes
% cla reset;
clrchild(h.axes1);
% 'XTick',[],'YTick',[], ...
set(h.axes1, ...
'Box','on', ...
'NextPlot','add',...
'DrawMode','fast' );
xlabel('feature x');
ylabel('feature y');
% No line
h.line.handler1=-1;
h.line.handler2=-1;
set(hfigure,'UserData',h);
% Get file name with sets
file=get(h.btload,'UserData');
% Load sets
sets=load(file.pathname);
% store loaded sets
set(h.axes1,'UserData',sets);
% set axes according to current points
win=cmpwin(min(sets.X'),max(sets.X'),BORDER,BORDER);
setaxis(h.axes1,win);
% axis(win);
% Appear point in 2D plane.
% points from set X1 and X2
% K=sum(sets.K);
% if K <= BIG_POINTS_NUM,
% set(h.xbsize,'Value',1);
% pointsize=BIG_POINT_SIZE;
% else
% set(h.xbsize,'Value',0);
% pointsize=SMALL_POINT_SIZE;
% end
% pplot(sets.X,sets.I,pointsize);
%%% pplot(sets.X,sets.I);
ppoints(sets.X,sets.I);
case 'play'
% == Start up the adaptation process =======================================
% Perform the adaptation step by step until the solution is found or stop
% button is pushed down.
h=get(hfigure,'UserData'); % get handlers
% Get sets
sets=get(h.axes1,'UserData');
% no data set loaded
if isempty(sets)==1,
return;
end
% Disable buttons everything axcept
set([h.btinfo h.btstep h.btclose h.btplay h.btreset h.btload h.btcreat h.pualgo ...
h.editer],'Enable','off');
% Only stop button can be pushed down
set(h.btstop,'Enable','on');
% Stop button was not pushed down
set(h.btstop,'UserData',0);
play=1;
% get parameters
t=h.line.t;
alpha=h.line.alpha;
fi=h.line.fi;
X=qtransf(sets.X);
% get # of iterations
iter=max(1,round(str2num(get(h.editer,'String'))));
% get epsilon
epsil=str2num(get(h.edeps,'String'));
% Play - adaptation process
while play==1 & get(h.btstop,'UserData')==0,
% perfor one adaptation step
switch get(h.pualgo,'Value')
case 1
[alpha,fi,solution,tplus1]=perceptr(X,sets.I,iter,t,alpha,fi);
case 2
[alpha,fi,solution,tplus1]=kozinec(X,sets.I,iter,t,alpha,fi);
case 3
[alpha,fi,solution,tplus1]=ekozinec(X,sets.I,epsil,iter,t,alpha,fi);
case 4
[alpha,fi,solution]=linsvm(X,sets.I);
if solution==0, solution=-1; end
tplus1=1;
end
t=tplus1;
if get(h.xbanim,'Value')==0,
quademo('drawline',hfigure,fi,alpha);
else
quademo('animline',hfigure,fi,alpha);
end
if solution==0,
% appear time and line parameters
text=makeinfo(t,alpha,fi,solution);
elseif solution==1,
% print result
text=makeinfo(t,alpha,fi,solution);
play=0;
elseif solution==-1,
play=0;
text=sprintf('Solution does not exist.\n');
end %
% appear text
set(h.console,'String',text );
% store new solution
h.line.t = t;
h.line.alpha = alpha;
h.line.fi = fi;
set(hfigure,'UserData',h);
end
% enable these buttons
set([h.btinfo h.btstep h.btclose h.btplay h.btreset h.pualgo ...
h.editer h.btload h.btcreat],...
'Enable','on');
% disable stop button
set(h.btstop,'Enable','off');
case 'step'
% == Perform one adaptation step ================================================
h=get(hfigure,'UserData'); % get handlers we will need...
% get sets
sets=get(h.axes1,'UserData');
% no data set loaded
if isempty(sets)==1,
return;
end
% get parameters
t=h.line.t;
alpha=h.line.alpha;
fi=h.line.fi;
X=qtransf(sets.X);
% get # of iter.
iter=max(1,round(str2num(get(h.editer,'String'))));
% get epsilon
epsil=str2num(get(h.edeps,'String'));
% perfor one adaptation step
switch get(h.pualgo,'Value')
case 1
[alpha,fi,solution,tplus1]=perceptr(X,sets.I,iter,t,alpha,fi);
case 2
[alpha,fi,solution,tplus1]=kozinec(X,sets.I,iter,t,alpha,fi);
case 3
[alpha,fi,solution,tplus1]=ekozinec(X,sets.I,epsil,iter,t,alpha,fi);
case 4
[alpha,fi,solution]=linsvm(X,sets.I);
if solution==0, solution=-1; end
tplus1=1;
end
t=tplus1;
if get(h.xbanim,'Value')==0,
quademo('drawline',hfigure,fi,alpha);
else
quademo('animline',hfigure,fi,alpha);
end
if solution==0 | solution==1,
% appear time and line parameters
text=makeinfo(t,alpha,fi,solution);
elseif solution==-1,
text=sprintf('Solution does not exist.\n');
end %
set(h.console,'String',text );
% store new solution
h.line.t = t;
h.line.alpha = alpha;
h.line.fi = fi;
set(hfigure,'UserData',h);
case 'animline'
% == Perform smooth transition of line from old to new position ==========
h=get(hfigure,'UserData'); % get handlers
% old position of line is...
alpha2=h.line.alpha;
fi2=h.line.fi;
t2=h.line.t;
% New position get from input arguments
fi1=varargin{1};
alpha1=varargin{2};
if t2~=0,
% move line
step=1/ANIM_STEPS;
for k=0:step:1,
alpha=(1-k)*alpha2+k*alpha1; % smooth transition of alpha
fi=(1-k)*fi2+k*fi1; % --//-- fi
quademo('drawline',hfigure,fi,alpha);
end
else
% it is first step
quademo('drawline',hfigure,fi1,alpha1); % first step
end % if t2~=0
case 'reset'
% == Reset adaptation process, set up zero step ================
h=get(hfigure,'UserData'); % get handlers
% get data set
sets=get(h.axes1,'UserData');
% get file
file=get(h.btload,'UserData');
% zeroize parameters of the separation line
h.line.t=0;
h.line.fi=0;
h.line.alpha=[0;0;0;0;0];
if h.line.handler1==-1,
h.line.handler1=plot([0],[0],'LineWidth',LINE_WIDTH,'EraseMode','xor','Color','k');
h.line.handler2=plot([0],[0],'LineWidth',LINE_WIDTH,'EraseMode','xor','Color','k');
else
set(h.line.handler1,'Visible','off');
set(h.line.handler2,'Visible','off');
end % if hline~=-1
% set up handlers and flush queue with graph. objects
set(hfigure,'UserData',h);
% creat comment
if isempty(sets)==0,
consoletext=sprintf('Step t=0\nNo separation line');
titletext=sprintf('File: %s, # of points K = %d',file.name,sum(sets.K));
else
consoletext=sprintf('No data loaded.\nPress Load data button.\n');
titletext='';
pos=get(h.axes1,'Position');
fsize=min(pos(3),pos(4))/10;
setaxis(h.axes1,[-1 1 -1 1]);
%%% axis([-1 1 -1 1]);
builtin('text',0,0,'Press ''Load data'' button.',...
'HorizontalAlignment','center',...
'FontUnits','normalized',...
'Clipping','on',...
'FontSize',fsize);
end
% show comment
set(h.console,'String',consoletext );
% print title
pos=get(h.axes1,'Position');
fsize=(1-pos(2)-pos(4))*1;
title(titletext,...
'VerticalAlignment','bottom',...
'HorizontalAlignment','left',...
'FontUnits','normalized',...
'Units','normalized',...
'Position',[0 1 0],...
'FontSize',fsize);
case 'drawline'
% == Draw separation line ==================================================
h=get(hfigure,'UserData'); % get handlers
% get new line position from input arguments
fi=varargin{1};
alpha=varargin{2};
[A,B,C]=l2q2d(alpha,fi);
%%% win=axis;
[X1,Y1,X2,Y2]=quad2d(A,B,C,getaxis(h.axes1));
% hide old curvatures
set(h.line.handler1,'Visible','off');
set(h.line.handler2,'Visible','off');
% appear new ones
set(h.line.handler1,'XData',X1,'YData',Y1,'Visible','on');
set(h.line.handler2,'XData',X2,'YData',Y2,'Visible','on');
% store data
set(hfigure,'UserData',h);
% flush draw queue
drawnow;
case 'info'
% == Call standard Matlab`s info box =========================================
helpwin(mfilename);
end
%========================================
function [text]=makeinfo(t,alpha,fi,solution)
[A,B,C]=l2q2d(alpha,fi);
if solution==1,
txline{1}=sprintf('Solution was found after t=%d step(s).',t);
else
txline{1}=sprintf('Step t=%d',t);
end
txline{2}=sprintf('Quadratic function:');
txline{3}=sprintf('%f x^2 + %f x + %f xy + %f y + %f y^2 + %f = 0',...
A(1,1),B(1),A(1,2)+A(2,1),B(2),A(2,2),C);
text='';
for i=1:3,
text=strvcat(text,txline{i});
end
return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -