📄 createdata.m
字号:
function result = createdata(action,varargin)
% 数据生成器,用户通过鼠标手动创建数据
% 说明:
% createdata 或者 createdata('finite') 可以创建两类二维的有限数据集,创建的数据分成两类(1,2)。
% 可以把创建的数据集存放到用户指定的目录中
% 文件表示如下:
% X [2 x num_data] 二维向量.
% y [1 x num_data] 数据类别标签.
%
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'
% == 创建有限数据集 ===================================
if nargin < 2,
K=2;
else
K=varargin{1};
end
createdata('initialize',action,K,varargin{2:nargin-1});
case 'initialize'
% == 初始化对话窗口 =========================================
% 获取输入变量个数get input arguments
K=varargin{2};
% == 图形窗口===========================================================
hfigure = figure(...
'Visible','off',...
'NumberTitle','off', ...
'Units','normalized', ...
'RendererMode','manual');
% == 坐标轴============================================================
haxes1= axes(...
'Units','normalized', ...
'ButtonDownFcn','createdata(''click'',gcf)',...
'Box','on', ...
'XGrid','on', ...
'YGrid','on', ...
'NextPlot','add',...
'Position',[0.1 0.1 0.65 0.85]);
axis([-1 1 -1 1]);
xlabel('X轴');
ylabel('Y轴');
% == 按钮 ==========================================================
left=0.78;
bottom=0.05;
height=0.08;
width=0.2;
% “关闭”按钮
hbtclose = uicontrol( ...
'Units','Normalized', ...
'Callback','createdata(''close'',gcf)',...
'ListboxTop',0, ...
'Position',[left bottom width height], ...
'FontSize',10,...
'String','关闭');
% “数据创建完毕”按钮
bottom=bottom+1*height;
hbtok = uicontrol( ...
'Units','Normalized', ...
'Callback','createdata(''ok'',gcf)',...
'ListboxTop',0, ...
'UserData',varargin,...
'Position',[left bottom width height], ...
'FontSize',10,...
'String','数据创建完毕');
% “关于创建数据”按钮
bottom=bottom+1.5*height;
hbtinfo = uicontrol( ...
'Units','Normalized', ...
'Callback','createdata(''info'',gcf)',...
'ListboxTop',0, ...
'Position',[left bottom width height], ...
'FontSize',10,...
'String','关于创建数据');
% “装载数据”按钮
bottom=bottom+1.5*height;
hbtload = uicontrol( ...
'Units','Normalized', ...
'Callback','createdata(''load'',gcf)',...
'ListboxTop',0, ...
'Position',[left bottom width height], ...
'FontSize',10,...
'String','装载数据');
% “保存数据”按钮
file=struct('name','noname.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,...
'FontSize',10,...
'String','保存数据');
% == 下拉菜单“数据类别”===================================================
%文本标签
bottom=0.91;
htxclass =uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',[left bottom width 0.6*height], ...
'FontSize',10,...
'String','数据类别');
%下拉菜单
for i=1:K,
txnum=sprintf(' %d ',i);
classes(i,1:size(txnum,2))=txnum;
end
bottom=bottom-0.5*height;
hpuclass=uicontrol( ...
'Style','popup', ...
'Units','normalized', ...
'Position',[left bottom width 0.6*height], ...
'String',classes);
% == 编辑框=======================================================
% 显示X轴区间
bottom=bottom-0.8*height;
htxxaxis=uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',[left bottom width 0.6*height], ...
'FontSize',10,...
'String','X轴');
bottom=bottom-0.5*height;
hedxaxis = uicontrol(...
'Units','normalized', ...
'ListboxTop',0, ...
'Position',[left bottom width 0.6*height], ...
'CallBack','createdata(''setaxis'',gcf)',...
'Style','edit',...
'FontSize',10,...
'String','[-1 1]');
% 显示Y轴区间
bottom=bottom-0.8*height;
htxyaxis=uicontrol( ...
'Style','text', ...
'Units','normalized', ...
'Position',[left bottom width 0.6*height], ...
'FontSize',10,...
'String','Y轴');
bottom=bottom-0.5*height;
hedyaxis = uicontrol(...
'Units','normalized', ...
'ListboxTop',0, ...
'Position',[left bottom width 0.6*height], ...
'CallBack','createdata(''setaxis'',gcf)',...
'Style','edit',...
'FontSize',10,...
'String','[-1 1]');
% == 坐标系上显示文件名 ========================================================
pos=get(haxes1,'Position');
titletext=sprintf('File: %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
handlers=struct(...
'settype',lower(varargin{1}),...
'saved',1,...
'btsave',hbtsave,...
'edxaxis',hedxaxis,...
'edyaxis',hedyaxis,...
'title',htitle,...
'btok',hbtok,...
'axes1',haxes1,...
'puclass',hpuclass );
end
sets=struct(...
'K',zeros(1,K),...
'X',[],...
'I',[],...
'N',2,...
'id',ident);
% 将handles传递到hfigure中
set(hfigure,'UserData',handlers);
% 将sets传递到haxes1中
set(haxes1,'UserData',sets);
% 将图形窗口可视属性 visible
set(hfigure,'Visible','on');
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');
end
%获取左右边界与最近点宽度,宽度之和大于1,则左右宽度取0.5,之和小于1,则取实际宽度之和的一半
dx=min( (maxs(1)-mins(1)), 1 )*DX_SPACE;
dy=min( (maxs(2)-mins(2)), 1 )*DY_SPACE;
%函数round取最接近的整数代替
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的定义在本文件最后
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','.', ...
'Color',XCOLORS(mod(sets.I(i)-1,MAXCOLOR)+1), ...
'MarkerSize',25, ...
'ButtonDownFcn','createdata(''click'',gcf)',...
'EraseMode','none',...
'Tag','point');
end
end % if strcmpi(...
% 设置新的坐标值
set(h.edxaxis,'String',sprintf('[%s]',num2str(win(1:2))) );%win=[x1 x2 x3 x4]
set(h.edyaxis,'String',sprintf('[%s]',num2str(win(3:4))) );
%设置类别
for i=1:max(size(sets.K)),
txnum=sprintf(' %d ',i);
classes(i,1:size(txnum,2))=txnum;
end
set(h.puclass,'String',classes);
case 'setaxis'
% == 获取X、Y轴编辑框中的区间信息设置X、Y轴==================
hfigure=varargin{1};
h = get(hfigure,'UserData'); % 获取句柄
xaxis=str2num(get(h.edxaxis,'String'));
yaxis=str2num(get(h.edyaxis,'String'));
if size(xaxis) ~= [1 2] | xaxis(2) <= xaxis(1),
errordlg('错误的X轴区间','错误','modal');
elseif size(yaxis) ~= [1 2] | yaxis(2) <= yaxis(1),
errordlg('错误的Y轴区间','错误','modal');
else
setaxis(h.axes1,[xaxis yaxis]);
end
case 'click'
% == 点击鼠标 =================================================
% 获取句柄
hfigure=varargin{1};
h=get(hfigure,'Userdata');
% 获取当前数据点的位置
pointer=get(h.axes1,'CurrentPoint');
% 获取数据集的结构体
sets=get(h.axes1,'UserData');
% 获取当前窗口的对象属性值
hobject=gco;
clicktype = lower(get(hfigure,'SelectionType'));
switch clicktype
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -