【GUI应用】Matlab实现矩阵计算器
界面设计
效果展示
主体源码
%%矩阵相加
a=str2num(get(handles.edit1,'string'));
b=str2num(get(handles.edit2,'string'));
[ia ja]=size(a);
[ib jb]=size(b);
if ia ~= ib | ja ~= jb |(ia ~= ib & ja ~= jb)
c='error.Matrix dimensions must agree.';
set(handles.text1,'String',c)
guidata(hObject, handles);
else
result = a+b ;
c= num2str(result);
set(handles.text1,'String',c)
guidata(hObject, handles);
end
%%矩阵相减
a=str2num(get(handles.edit1,'string'));
b=str2num(get(handles.edit2,'string'));
[ia ja]=size(a);
[ib jb]=size(b);
if ia ~= ib | ja ~= jb |(ia ~= ib & ja ~= jb)
c='error.Matrix dimensions must agree.';
set(handles.text1,'String',c)
guidata(hObject, handles);
else
result = a-b ;
c= num2str(result);
set(handles.text1,'String',c)
guidata(hObject, handles);
end
%%矩阵点乘
a=str2num(get(handles.edit1,'string'));
b=str2num(get(handles.edit2,'string'));
[ia ja]=size(a);
[ib jb]=size(b);
if ia ~= ib | ja ~= jb |(ia ~= ib & ja ~= jb)
c='error.Matrix dimensions must agree.';
set(handles.text1,'String',c)
guidata(hObject, handles);
else
result = a.*b ;
c= num2str(result);
set(handles.text1,'String',c)
guidata(hObject, handles);
end
%%矩阵相乘
a=str2num(get(handles.edit1,'string'));
b=str2num(get(handles.edit2,'string'));
[ia ja]=size(a);
[ib jb]=size(b);
if ja ~= ib
c='error.Inner matrix dimensions must agree.';
set(handles.text1,'String',c)
guidata(hObject, handles);
else
result = a*b ;
c= num2str(result);
set(handles.text1,'String',c)
guidata(hObject, handles);
end
%%矩阵点除
a=str2num(get(handles.edit1,'string'));
b=str2num(get(handles.edit2,'string'));
[ia ja]=size(a);
[ib jb]=size(b);
if ia ~= ib | ja ~= jb |(ia ~= ib & ja ~= jb)
c='error.Matrix dimensions must agree.';
set(handles.text1,'String',c)
guidata(hObject, handles);
else
result = a./b ;
c= num2str(result);
set(handles.text1,'String',c)
guidata(hObject, handles);
end
%%矩阵左除
a=str2num(get(handles.edit1,'string'));
b=str2num(get(handles.edit2,'string'));
[ia ja]=size(a);
[ib jb]=size(b);
if ia ~= ib
c='error.Matrix dimensions must agree.';
set(handles.text1,'String',c)
guidata(hObject, handles);
else
result = a\b ;
c= num2str(result);
set(handles.text1,'String',c)
guidata(hObject, handles);
end
%%矩阵右除
a=str2num(get(handles.edit1,'string'));
b=str2num(get(handles.edit2,'string'));
[ia ja]=size(a);
[ib jb]=size(b);
if ia ~= ib
c='error.Matrix dimensions must agree.';
set(handles.text1,'String',c)
guidata(hObject, handles);
else
result = a/b ;
c= num2str(result);
set(handles.text1,'String',c)
guidata(hObject, handles);
end
%%矩阵求秩
a=str2num(get(handles.edit1,'string'));
c=a';
set(handles.text1,'string',num2str(c))
a=str2num(get(handles.edit1,'string'));
result =rank(a) ;
c= num2str(result);
set(handles.text1,'String',c)
guidata(hObject, handles);
%%求逆
a=str2num(get(handles.edit1,'string'));
[ia ja]=size(a);
if ia~= ja
c='error.Matrix dimensions must agree.';
set(handles.text1,'String',c)
guidata(hObject, handles);
else
if abs(det(a))<1e-6
c='error.Matrix is singular to working precision.';
set(handles.text1,'String',c)
guidata(hObject, handles);
else
result = inv(a) ;
c = num2str(result);
set(handles.text1,'String',c)
guidata(hObject, handles);
end
end
%%求行列式
a=str2num(get(handles.edit1,'string'));
[ia ja]=size(a);
if ia ~= ja
c='error.Matrix dimensions must agree.';
set(handles.text1,'String',c)
guidata(hObject, handles);
else
result = det(a) ;
c= num2str(result);
set(handles.text1,'String',c)
guidata(hObject, handles);
end
%%求2-范数
a=str2num(get(handles.edit1,'string'));
result = norm(a);
c = num2str(result);
set(handles.text1,'String',c)
guidata(hObject, handles)
%%求LU分解
a=str2num(get(handles.edit1,'string'));
[L,U,P]=lu(a);
result = [L;U;P] ;
c= num2str(result);
set(handles.text1,'String',c)
guidata(hObject, handles)
%%求AX=B
a=str2num(get(handles.edit1,'string'));
b=str2num(get(handles.edit2,'string'));
[ia ja]=size(a);
[ib jb]=size(b);
if ia ~= ib
c='error.';
set(handles.text1,'String',c)
guidata(hObject, handles);
else
if ia ~=ja
c='error.Matrix must be square.';
set(handles.text1,'String',c)
guidata(hObject, handles);
else
if det(a) == 0
c='error.Matrix is singular to working precision.';
set(handles.text1,'String',c)
guidata(hObject, handles);
else
result = a\b ;
c = num2str(result);
set(handles.text1,'String',c)
guidata(hObject, handles);
end
end
end
a=str2num(get(handles.edit1,'string'));
result = rref(a);
c= num2str(result);
set(handles.text1,'String',c)
guidata(hObject, handles);
%%求特征值
a=str2num(get(handles.edit1,'string'));
[ia ja]=size(a);
if ia ~= ja
c='error.Matrix must be square .';
set(handles.text1,'String',c)
guidata(hObject, handles);
else
result = eig(a) ;
c= num2str(result);
set(handles.text1,'String',c)
guidata(hObject, handles);
end
%阶梯形
a=str2num(get(handles.edit1,'string'));
b=str2num(get(handles.edit2,'string'));
[ia ja]=size(a);
[ib jb]=size(b);
if ia ~= ib
c='error.Matrix dimensions must agree.';
set(handles.edit34,'String',c)
guidata(hObject, handles);
else
result = rref(a) ;
c= num2str(result);
set(handles.edit34,'String',c)
guidata(hObject, handles);
end
%%求转置
a=str2num(get(handles.edit1,'string'));
[ia ja]=size(a);
if ia ~= ja
c='error.Matrix must be square .';
set(handles.edit34,'String',c)
guidata(hObject, handles);
else
result =a' ;
c= num2str(result);
set(handles.edit34,'String',c)
guidata(hObject, handles);
end
菜单结构设计
菜单的设计只做点拨,不要照搬!!!通过Menu Editor创建如图所示的菜单。“File”菜单调用简易计算器、科学计算器、程序员计算器及退出;“Help”菜单调用程序说明和版权所有。
“File”菜单设计
通过设置简易计算器、科学计算器和程序员计算器所在面板的可视性属性,实现点击“File”菜单下的“简易计算器”、“科学计算器”和“程序员计算器”选项调用相应的计算器模块。调用简易计算器模块时,只需设置简易计算器所在面板的可视化属性值为“on”,其他计算器的可视化属性值为“off”;设置“简易计算器”菜单项的被选中(checked)属性值为“on”,其余菜单项的checked属性值为“off”。其余模块调用的原理与其类似。代码如下:
function simple_Callback(hObject, eventdata, handles)
set(handles.science_clc,'Visible','off');
set(handles.simple_clc,'Visible','on');
set(handles.clc_p,'Visible','off');
set(handles.jy_clc,'Position',[103.8 20.769 84.0 33.692]);
set(handles.simple,'checked','on');
set(handles.science,'checked','off');
set(handles.programmer,'checked','off');