📄 createdata.m
字号:
function result = createdata(action,varargin)
% CREATEDATA Interactive data generator.
%
% Synopsis:
% createdata
% createdata('finite')
% createdata('finite',num_classes)
% createdata('gauss')
% createdata('gauss',num_distrib)
%
% Description:
% createdata or createdata('finite') invokes an interactive generator
% of finite point sets in 2D. The generated points can be assigned
% into two classes (1,2). The generator saves data to a
% specified file. The file has the following fields:
% X [2 x num_data] 2D vectors.
% y [1 x num_data] Assigned labels.
%
% createdata('finite',num_classes) when num_classes is specified
% then the points can be assigned labels from 1 to num_classes.
%
% createdata('gauss') invokes an interactive generator of
% Gaussian distributions. A user can specify mean vector and
% covariance matrix. The generator saves data to a file
% having the following fields:
% Mean [2 x ncomp] Mean vectors.
% Cov [2 x 2 x ncomp] Covariance matrices.
% y [1 x ncomp] Assigned labels.
%
% createdata('gauss',num_classes) when num_classes is specified
% then the distributions can be assigned labels from 1 to num_classes.
%
% The data generator is controlled by mouse:
% Left button ... creates a new point or Gaussian.
% Right button ... erases the focused point or Gaussian.
%
% In the case of generating Gaussians left button double-click makes
% selected Gaussian focused. The covariance matrix of focused
% Gaussian can be modified.
%
% About: Statistical Pattern Recognition Toolbox
% (C) 1999-2003, Written by Vojtech Franc and Vaclav Hlavac
% <a href="http://www.cvut.cz">Czech Technical University Prague</a>
% <a href="http://www.feld.cvut.cz">Faculty of Electrical Engineering</a>
% <a href="http://cmp.felk.cvut.cz">Center for Machine Perception</a>
XCOLORS=['r','b','g','m','c','k','y']; % 设置椭圆的颜色
MAXCOLOR=size(XCOLORS,2);
ID_NORMAL='Infinite sets, Normal distributions';
ID_FINITE='Finite sets, Enumeration';
DX_SPACE=0.5; % 左右边界及相邻两点之间的宽度
DY_SPACE=0.5;
if nargin < 1,
action = 'finite';
end
switch lower(action)
case 'finite'
% 创建finite数据集
if nargin < 2,
K=2;
else
K=varargin{1};
end
createdata('initialize',action,K,varargin{2:nargin-1});
case 'gauss'
% 创建gaussian数据集
if nargin < 2,
K=2;
else
K=varargin{1};
end
createdata('initialize',action,K,varargin{2:nargin-1});
case 'initialize' %初始化对话窗口
K=varargin{2}; %获得输入参数个数
%定义用户界面
hfigure = figure(...
'Visible','off',...
'NumberTitle','off', ...
'Units','normalized', ...
'RendererMode','manual');
%定义轴
haxes1= axes(...
'Units','normalized', ...
'ButtonDownFcn','createdata(''click'',gcf)',...
'Box','off', ...
'XGrid','off', ...
'YGrid','off', ...
'NextPlot','add',...
'Position',[0.1 0.1 0.65 0.85]);
axis([-1 1 -1 1]);
xlabel('Feature X');
ylabel('Feature Y');
% 定义功能按钮大小
left=0.8;
bottom=0.1;
height=0.06;
width=0.16;
%“退出”按钮
hbtclose = uicontrol( ...
'Units','Normalized', ...
'Callback','createdata(''close'',gcf)',...
'ListboxTop',0, ...
'Position',[left bottom width height], ...
'String','退出');
%“确定”按钮
bottom=bottom+1.5*height;
hbtok = uicontrol( ...
'Units','Normalized', ...
'Callback','createdata(''ok'',gcf)',...
'ListboxTop',0, ...
'UserData',varargin,...
'Position',[left bottom width height], ...
'String','确定');
%“关于”按钮
bottom=bottom+1.5*height;
hbtinfo = uicontrol( ...
'Units','Normalized', ...
'Callback','createdata(''info'',gcf)',...
'ListboxTop',0, ...
'Position',[left bottom width height], ...
'String','关于');
%“装载”按钮
bottom=bottom+1.5*height;
hbtload = uicontrol( ...
'Units','Normalized', ...
'Callback','createdata(''load'',gcf)',...
'ListboxTop',0, ...
'Position',[left bottom width height], ...
'String','装载');
%“保存”按钮
bottom=bottom+0.5*height;
file=struct('name','*.mat','path','','pathname','noname.mat');
bottom=bottom+1*height;
hbtsave = uicontrol( ...
'Units','Normalized', ...
'Callback','createdata(''save'',gcf)',...
'ListboxTop',0, ...
'Position',[left bottom width height], ...
'UserData',file,...
'String','保存');
% 定义下拉菜单
%定义标记:“数据类”
bottom=0.91;
htxclass =uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',[left bottom width 0.9*height], ...
'String','数据类');
% 下拉菜单
for i=1:K,
txnum=sprintf(' %d ',i);
classes(i,1:size(txnum,2))=txnum;
end
bottom=bottom-height;
hpuclass=uicontrol( ...
'Style','popup', ...
'Units','normalized', ...
'Position',[left bottom width height], ...
'String',classes);
%定义标记
%“X轴”
bottom=bottom-1.2*height;
htxxaxis=uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',[left bottom width 0.9*height], ...
'String','X轴');
bottom=bottom-height;
hedxaxis = uicontrol(...
'Units','normalized', ...
'ListboxTop',0, ...
'Position',[left bottom width height], ...
'CallBack','createdata(''setaxis'',gcf)',...
'Style','edit',...
'String','[-1 1]');
%“Y轴”
bottom=bottom-1.2*height;
htxyaxis=uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',[left bottom width 0.9*height], ...
'String','Y轴');
bottom=bottom-height;
hedyaxis = uicontrol(...
'Units','normalized', ...
'ListboxTop',0, ...
'Position',[left bottom width height], ...
'CallBack','createdata(''setaxis'',gcf)',...
'Style','edit',...
'String','[-1 1]');
if strcmpi(varargin{1},'gauss'),
%标记“协方差”
bottom=bottom-1.5*height;
htxcov=uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',[left bottom width 0.9*height], ...
'String','协方差');
% cov(xy)
bottom=bottom-height;
hedxx = uicontrol(...
'Units','normalized', ...
'ListboxTop',0, ...
'Position',[left bottom width*0.5 height], ...
'CallBack','createdata(''setcov'',gcf,1)',...
'Style','edit',...
'String','1');
% cov(xy)
hedxy = uicontrol(...
'Units','normalized', ...
'ListboxTop',0, ...
'Position',[left+width*0.5 bottom width*0.5 height], ...
'CallBack','createdata(''setcov'',gcf,2)',...
'Style','edit',...
'String','0');
% cov(yx)
bottom=bottom-height;
hedyx = uicontrol(...
'Units','normalized', ...
'ListboxTop',0, ...
'Position',[left bottom width*0.5 height], ...
'CallBack','createdata(''setcov'',gcf,3)',...
'Style','edit',...
'String','0');
% cov(yy)
hedyy = uicontrol(...
'Units','normalized', ...
'ListboxTop',0, ...
'Position',[left+width*0.5 bottom width*0.5 height], ...
'CallBack','createdata(''setcov'',gcf,4)',...
'Style','edit',...
'String','1');
bottom=bottom-1.2*height;
htxmi1=uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',[left bottom width 0.9*height], ...
'String','MI=[');
bottom=bottom-0.9*height;
htxmi2=uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',[left bottom width 0.9*height], ...
'String',' ]');
end
% 标记坐标轴
pos=get(haxes1,'Position');
titletext=sprintf('文件: %s',file.name);
fontsize=(1-pos(2)-pos(4))*0.8;
htitle=title(titletext,...
'VerticalAlignment','bottom',...
'HorizontalAlignment','left',...
'FontUnits','normalized',...
'Units','normalized',...
'Position',[0 1 0],...
'FontSize',fontsize);
% 选择当前的数据集类型创建数据
switch lower(varargin{1}),
case 'finite'
set(hfigure,'name','创建数据');
ident=ID_FINITE;
% 设置句柄
handlers=struct(...
'settype',lower(varargin{1}),...
'saved',1,...
'btsave',hbtsave,...
'edxaxis',hedxaxis,...
'edyaxis',hedyaxis,...
'title',htitle,...
'btok',hbtok,...
'axes1',haxes1,...
'puclass',hpuclass );
case 'gauss'
set(hfigure,'name','创建高斯数据');
ident=ID_NORMAL;
handlers=struct(...
'settype',lower(varargin{1}),...
'currpoint',0,...
'currhandle',0,...
'saved',1,...
'title',htitle,...
'btsave',hbtsave,...
'edxaxis',hedxaxis,...
'edyaxis',hedyaxis,...
'txmi1',htxmi1,...
'txmi2',htxmi2,...
'edxx',hedxx,...
'edxy',hedxy,...
'edyx',hedyx,...
'edyy',hedyy,...
'btok',hbtok,...
'axes1',haxes1,...
'puclass',hpuclass );
end
sets=struct(...
'K',zeros(1,K),...
'X',[],...
'I',[],...
'MI',[],...
'SIGMA',[],...
'N',2,...
'id',ident);
% 保存句柄和结构型数据
set(hfigure,'UserData',handlers);
set(haxes1,'UserData',sets);
%使图形可视化
set(hfigure,'Visible','on');
case 'setcov'
% 获得句柄
hfigure=varargin{1};
h=get(hfigure,'UserData');
%获取数据集
sets=get(h.axes1,'UserData');
%当前的数据点
i=h.currpoint;
if varargin{2}==2,
set(h.edyx,'String',get(h.edxy,'String'));
elseif varargin{2}==3,
set(h.edxy,'String',get(h.edyx,'String'));
end
if i ~= 0,
sigma(1,1)=str2num(get(h.edxx,'String'));
sigma(1,2)=str2num(get(h.edxy,'String'));
sigma(2,1)=str2num(get(h.edyx,'String'));
sigma(2,2)=str2num(get(h.edyy,'String'));
[aa,p]=chol(sigma);
if p ~= 0,
set(h.edxx,'String','1');
set(h.edxy,'String','0');
set(h.edyx,'String','0');
set(h.edyy,'String','1');
sigma=eye(2,2);
end
sets.SIGMA(:,(i-1)*2+1:i*2)=sigma;
set(h.axes1,'UserData',sets);
window=getaxis(h.axes1);
R=min([(window(2)-window(1)),(window(4)-window(3))])/20;
i=h.currpoint;
class=sets.I(i);
mi=sets.MI(:,i);
[x,y]=ellips(mi,inv(sigma),R);
if h.currhandle==0,
% 绘制新的椭圆
h.currhandle =fill(x,y,XCOLORS(mod(class-1,MAXCOLOR)+1),...
'EraseMode','none',...
'ButtonDownFcn','createdata(''click'',gcf)',...
'Tag','ellipse',...
'UserData',mi);
set(hfigure,'UserData',h);
else
set(h.currhandle,'XData',x,'YData',y,'EraseMode','normal');
end
end
case 'redraw'
%重新绘制坐标轴
hfigure=varargin{1};
h = get(hfigure,'UserData'); %获取句柄
children=get(h.axes1,'Children' );
set(children,'EraseMode','normal','Visible','off');
%获取数据集
sets=get(h.axes1,'UserData');
% 设置坐标轴
if strcmpi(sets.id,ID_FINITE)==1,
maxs=max(sets.X');
mins=min(sets.X');
elseif strcmpi(sets.id,ID_NORMAL )==1,
maxs=max(sets.MI');
mins=min(sets.MI');
end
dx=min( (maxs(1)-mins(1)), 1 )*DX_SPACE;
dy=min( (maxs(2)-mins(2)), 1 )*DY_SPACE;
x1=round(mins(1)-dx);
x2=round(maxs(1)+dx);
y1=round(mins(2)-dy);
y2=round(maxs(2)+dx);
win=[x1 x2 y1 y2];
axes(h.axes1);
setaxis(h.axes1,win);
%重新绘制点或椭圆
if strcmpi(sets.id,ID_FINITE)==1,
% 点集
for i=1:sum(sets.K),
line(sets.X(1,i),sets.X(2,i), ...
'LineStyle','none', ...
'Marker','.', ...
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -