程序运行图形输出:
图3 运行界面
图4 连杆3的几个位臵点
12
图5 角位移线图
图6 角速度线图
13
图7 角加速度线图 4 基于MATLAB图形界面设计
4.1 界面设计
图8 新建GUI文件
首先我们新建一个GUI文件,如上图所示,选择Blank GUI(Default):
14
进入GUI开发环境以后添加5个编辑文本框,9个静态文本框,和1个下拉菜单。利用菜单编辑器,创建Open、Print、Close三个菜单。创建好GUI界面需要的各交互控件并调整好位臵后,设臵这些控件的属性。最后的界面如图示:
图9 界面效果
4.2 代码设计
(1)打开M文件编辑器(M-file Edit),点击
向下的三角图标,可以看到各个
对象的回调函数(Callback),某些对象的创建函数或打开函数等。通过选中相应
项就可以跳动对应函数位臵进行程序编辑。
选中’edit_callback’选项,光标跳到’ function edit1_Callback(hObject, eventdata, handles)’下面空白处,添加以下代码:
user_entry=str2double(get(hObject,'String')); if isnan(user_entry)
errordlg('请输入数值!','Bad Input') end
该语句严格限制编辑框内必须输入数值,否则出现错误对话框(如下图所示)。同理在其他四个编辑框的回调函数下输入相同的代码。
图10 错误对话框
15
(2)打开M文件编辑器(M-file Edit),点击向下的三角图标,设臵下拉菜单
返回函数,光标跳到’ function popupmenu1_Callback(hObject, eventdata,
handles)’下面空白处。由于下拉菜单是本界面设计关键控件,与本设计相关的程序都放在这个返回函数下。添加代码如下:L1=str2double(get(handles.edit1,'String'));
L2=str2double(get(handles.edit2,'String')); L3=str2double(get(handles.edit3,'String')); L4=str2double(get(handles.edit4,'String')); w2=str2double(get(handles.edit5,'String')); th2=[0:2/72:2]*pi;
th34=zeros(length(th2),2);
options=optimset('display','off'); for m=1:length(th2)
th34(m,:)=fsolve('aa',[1 1],options,th2(m),L2,L3,L4,L1); end w2=250;
for i=1:length(th2) A=[-L3*sin(th34(i,1)) L4*sin(th34(i,2)); L3*cos(th34(i,1)) -L4*cos(th34(i,2))];
B=[w2*L2*sin(th2(i)); -w2*L2*cos(th2(i))]; w=inv(A)*B; w3(i)=w(1); w4(i)=w(2); end
for i=1:length(th2) C=[-L3*sin(th34(i,1)) L4*sin(th34(i,2));L3*cos(th34(i,1)) -L4*cos(th34(i,2))];
D=[w2^2*L2*cos(th2(i))+w3(i)^2*L3*cos(th34(i,1))-w4(i)^2*L4*cos(th34(i,2));...
w2^2*L2*sin(th2(i))+w3(i)^2*L3*sin(th34(i,1))-w4(i)^2*L4*sin(th34(i,2))];
a=inv(C)*D; a3(i)=a(1); a4(i)=a(2); end
val=get(hObject,'Value'); str=get(hObject,'String'); switch str{val}
case '连杆3的几个位臵点' th2=[0:1/6:2]*pi;
th34=zeros(length(th2),2);
options=optimset('display','off'); for m=1:length(th2)
16