Matlab 手写输入板
因为 想做个手写数字识别,就先做了这手写输入的面板,这是我根据
matlab 中文论坛里的一篇文档写的。M 文件比较麻烦,删掉了些注释。
下面是 GUI 和 M 文件
M文件直接从控件开始吧
% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(hObject, eventdata, handles)
global draw_enable
global x;
global y;
global h1;
draw_enable=1;
if draw_enable==1
%定义一个标志,1表示绘图,0表示停止绘图
p=get(gca,'currentpoint');
x(1)=p(1);
y(1)=p(3);
end
%鼠标按下,获取当前坐标
% --- Executes on mouse motion over figure - except title and menu.
function figure1_WindowButtonMotionFcn(hObject, eventdata, handles)
global draw_enable;
global x;
global y;
global h1;
p=get(gca,'currentpoint');
if draw_enable==1
x(2)=p(1);
y(2)=p(3);
h1=line(x,y,'EraseMode','xor','LineWidth',5,'color','b');
%鼠标第一次移动后的坐标为x(2),y(2)
x(1)=x(2);
y(1)=y(2);
end
%鼠标移动时,随时更新折线的数据
% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonUpFcn(hObject, eventdata, handles)
global draw_enable;
draw_enable=0;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
axes(handles.axes1);
cla;
axes('Position',[19.8 12.923 70.2 15.462],'tag','axes1');
%清空,但cla是清楚坐标抽,故在下面又建立一个坐标轴,且取tag名为axes1
效果图