Matlab 手写板设计

发布于:2024-05-07 ⋅ 阅读:(15) ⋅ 点赞:(0)

1、介绍 

    MATLAB手写板可以作为一个很好的数据输入口,其可以获取该手写板上任意字母、数字,甚至可以制作样本数据。具体用途体现在如下几方面:

  1. 数学公式输入:手写板允许用户直接用手写方式输入复杂的数学公式,这对于使用键盘输入困难的公式尤其有用。

  2. 图形绘制:在进行图形学研究或需要绘制精确图形时,手写板可以提供更直观和精确的绘图方式。

  3. 交互式演示:在教学或演示中,手写板可以用于实时绘制图形、图表或数学表达式,增强互动性和演示效果。

  4. 数据标记和注释:在分析数据和结果时,手写板可以用来在图形上直接添加注释或标记重点。

  5. 个性化界面:一些用户可能会发现使用手写板比传统的鼠标和键盘更符合个人习惯,从而提高工作效率。

  6. 快速原型设计:在进行算法或模型的快速原型设计时,手写板可以快速记录和测试初步想法。

  7. 辅助工具:对于有特殊需要的用户,如行动不便或更喜欢手写输入的用户,手写板可以作为一个辅助工具。

    2、制作方法

     在matlab中,可以在其GUI界面中,制作写字板。具体过程如下:

2.1 创建GUI界面

      在命令行窗口中,输入“guide”命令,回车,弹出“GUIDE快速入门”,创建一个空白GUI界面即可。

2.2 GUI界面控件布设

     制作GUI界面时,需要3类控件:坐标轴、按钮组、按钮。其中坐标轴是手写区域,按钮组主要为了美观,提示用于该区域的功能,两个按钮:“重置”、“保存”,分别用于清空手写区域,以及对手写区域图片进行保存。再按钮组、按钮上名字进行修改,便于用于理解,具体布设如下图所示:

   由于axes默认室友刻度线的,因此需要对其属性进行设置,将刻度线删除掉。分别为:

xcolor、ycolor设置成白色

xticklabel、yticklabel里的刻度删掉。

2.3 代码添加

在创建好GUI界面后,再根据需要撰写代码即可。其中,不选中控件,右击进行添加代码。

(1)获取鼠标位置:

% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global draw_enable;
global x;
global y;
set(handles.axes1,'XLim',[0,1],'YLim',[0,1]);%防止开始时鼠标乱动
draw_enable=1;
if draw_enable
    position=get(gca,'currentpoint');
    x(1)=position(1);
    y(1)=position(3);
end

(2)更新鼠标位置并画线(鼠标在按下的情况下运动)

% --- Executes on mouse motion over figure - except title and menu.
function figure1_WindowButtonMotionFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global draw_enable;
global x;
global y;
if draw_enable
    position=get(gca,'currentpoint');
    x(2)=position(1);
    y(2)=position(3);
    line(x,y,'EraseMode','xor','LineWidth',5,'color','b');
    x(1)=x(2);
    y(1)=y(2);
end

(3)鼠标放下后停止画线

% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonUpFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global draw_enable
draw_enable=0;

(4)重置按钮

重置是为了将axes中内容清空,代码如下:

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.axes3);
cla reset;
set(handles.axes3,'xtick',[]);
set(handles.axes3,'ytick',[]);
set(handles.axes3,'xcolor','w');
set(handles.axes3,'ycolor','w');

(5)保存按钮

保存按钮是将写字板中内容进行保存。

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
h=getframe(handles.axes3);
[filename,pathname,fileindex]=uiputfile('*.bmp','图片另存为');
file_path=[pathname filename];
imwrite(h.cdata,file_path,'bmp');
cla(handles.axes3);

3、实验结果

测试结果如下,可以发现本程序可以实现手写输入、图片保存与手写板清空。

GUI界面 手写字母a 手写字母b
重置 图片保存 保存图片打开

4、源代码使用教程

若不想自己手写代码,可以直接下载源代码,下载链接:https://download.csdn.net/download/qq_32867925/89272940

包括两个文件:m文件与fig文件,如下:

  要想运行该程序,在matlab命令行窗口中输入“guide”,在弹出的对话框中选择“writeboard.fig”。

  点击绿色三角形,即可进行运行。