Matlab程序.docx
- 文档编号:18150204
- 上传时间:2023-08-13
- 格式:DOCX
- 页数:19
- 大小:70.64KB
Matlab程序.docx
《Matlab程序.docx》由会员分享,可在线阅读,更多相关《Matlab程序.docx(19页珍藏版)》请在冰点文库上搜索。
Matlab程序
实验报告
实验任务:
1.创建函数实现输入任意一个字符串向量,然后对该向量做如下处理:
(1)取第(5-9,序号为单数的;1-5,序号为双数的)个字符组成的子字符串。
(2)取字符串倒过来重新排列。
(3)将字符串中的小写字母变成相应的大写字母(学号小于等于15),大写字母变成相应的小写字母(学号大于等于15),其余字符不变。
(4)统计字符串中小写字母的个数(序号尾数为5),统计字符串中大写字母的个数(其他的序号)
注:
以字符串’ABcd5eFGhij456YmN’为例说明。
a='ABcd5eFGhij456YmN';
%b=a([24579]);
b=a([2:
2:
4,5:
2:
9]);
n=length(a);
c=a(n:
-1:
1);
j=0;
k=0;
i=1:
n;
fori=1:
15
ifa(i)>='a'&a(i)<='z'
d(i)=upper(a(i));
else
d(i)=a(i);
end
end
fori=16:
n
ifa(i)>='A'&a(i)<='Z'
d(i)=lower(a(i));
else
d(i)=a(i);
end
end
fori=5:
10:
n
ifa(i)>='a'&a(i)<='z'
j=j+1;
else
j=j;
end
end
fori=1:
n
if(a(i)>='A'&a(i)<='Z')
ifrem(i,5)==0
k=k;
else
k=k+1;
end
end
end
a,b,c,d,j,k
结果:
a=
ABcd5eFGhij456YmN
b=
Bd5Fh
c=
NmY654jihGFe5dcBA
d=
ABCD5EFGHIJ456Ymn
j=
0
k=
5
出现错误:
Errorin==>C:
\DocumentsandSettings\Owner\桌面\a\s1.m
Online2==>b=a([2:
2:
4],[5:
2:
9]);
从a中提取字符时格式出错。
2.输入矩阵a为
,分别抽取其第2条对角线元素,创建对角矩阵,抽取下三角矩阵。
a=[1111;1234;13610;141020];
b=diag(a,2);
c=diag(b,2);
d=tril(a,2);
a,b,c,d
结果:
a=
1111
1234
13610
141020
b=
1
4
c=
0010
0004
0000
0000
d=
1110
1234
13610
141020
3.再同一坐标内分别用不同线型和颜色绘制曲线,标记横纵坐标和图标题。
注:
设x的范围为(0,2∏)且为1000个计数点,
要求:
序号尾数为1,3,6,9的。
Y1曲线为红色,y2曲线为绿色冒号线,标记点为蓝色菱形。
序号尾数为2,4,8的。
Y1曲线为黑色虚线,y2曲线为蓝色点划线,标记点为红色菱形。
序号尾数为0,5,7的。
Y1曲线为紫色实线,y2曲线为黑色点划线,标记点为红色五角星。
x=0:
2*pi/999:
2*pi;
y1=5*exp(-0.5*x).*cos(4*pi*x);
y2=0.5*exp(-x).*cos(pi*x);
a=0;b=0;c=0;d=0;
e=0;f=0;g=0;h=0;
fori=1:
1000
ifabs(y1(i)-y2(i))<1e-2
ifrem(i,10)==1|rem(i,10)==3|rem(i,10)==6|rem(i,10)==9
b=b+1;
x01(b)=x(i);
y01(b)=y1(i)
elseifrem(i,10)==2|rem(i,10)==4|rem(i,10)==8
c=c+1;
x02(c)=x(i);
y02(c)=y1(i);
elseifrem(i,10)==0|rem(i,10)==5|rem(i,10)==7
d=d+1;
x03(d)=x(i);
y03(d)=y1(i);
end
else
ifrem(i,10)==1|rem(i,10)==3|rem(i,10)==6|rem(i,10)==9
f=f+1;
x1(f)=x(i);
y11(f)=y1(i);
y21(f)=y2(i);
elseifrem(i,10)==2|rem(i,10)==4|rem(i,10)==8
g=g+1;
x2(g)=x(i);
y12(g)=y1(i);
y22(g)=y2(i);
elseifrem(i,10)==0|rem(i,10)==5|rem(i,10)==7
h=h+1;
x3(h)=x(i);
y13(h)=y1(i);
y23(h)=y2(i);
end
end
end
plot(x01,y01,'bd',x02,y02,'rd',x03,y03,'rp',x1,y11,'r',x2,y12,'k--',x3,y13,'m-',x1,y21,'g:
',x2,y22,'b-.',x3,y23,'k-.');
xlabel('X');
ylabel('Y');
axis([02*pi-55]);
title('quxiantu');
结果:
4.使用GUI实现计算器的功能,计算器的截图如下,描述出具体的操作步骤,界面控件的设置及其代码的编写。
functionvarargout=s4(varargin)
%S4M-filefors4.fig
%S4,byitself,createsanewS4orraisestheexisting
%singleton*.
%
%H=S4returnsthehandletoanewS4orthehandleto
%theexistingsingleton*.
%
%S4('Property','Value',...)createsanewS4usingthe
%givenpropertyvaluepairs.Unrecognizedpropertiesarepassedvia
%varargintos4_OpeningFcn.Thiscallingsyntaxproducesa
%warningwhenthereisanexistingsingleton*.
%
%S4('CALLBACK')andS4('CALLBACK',hObject,...)callthe
%localfunctionnamedCALLBACKinS4.Mwiththegiveninput
%arguments.
%
%*SeeGUIOptionsonGUIDE'sToolsmenu.Choose"GUIallowsonlyone
%instancetorun(singleton)".
%
%Seealso:
GUIDE,GUIDATA,GUIHANDLES
%Edittheabovetexttomodifytheresponsetohelps4
%LastModifiedbyGUIDEv2.519-Dec-201220:
33:
51
%Begininitializationcode-DONOTEDIT
gui_Singleton=1;
gui_State=struct('gui_Name',mfilename,...
'gui_Singleton',gui_Singleton,...
'gui_OpeningFcn',@s4_OpeningFcn,...
'gui_OutputFcn',@s4_OutputFcn,...
'gui_LayoutFcn',[],...
'gui_Callback',[]);
ifnargin&isstr(varargin{1})
gui_State.gui_Callback=str2func(varargin{1});
end
ifnargout
[varargout{1:
nargout}]=gui_mainfcn(gui_State,varargin{:
});
else
gui_mainfcn(gui_State,varargin{:
});
end
%Endinitializationcode-DONOTEDIT
%---Executesjustbefores4ismadevisible.
functions4_OpeningFcn(hObject,eventdata,handles,varargin)
%Thisfunctionhasnooutputargs,seeOutputFcn.
%hObjecthandletofigure
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%vararginunrecognizedPropertyName/PropertyValuepairsfromthe
%commandline(seeVARARGIN)
%Choosedefaultcommandlineoutputfors4
handles.output=hObject;
%Updatehandlesstructure
guidata(hObject,handles);
%UIWAITmakess4waitforuserresponse(seeUIRESUME)
%uiwait(handles.figure1);
%---Outputsfromthisfunctionarereturnedtothecommandline.
functionvarargout=s4_OutputFcn(hObject,eventdata,handles)
%varargoutcellarrayforreturningoutputargs(seeVARARGOUT);
%hObjecthandletofigure
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%Getdefaultcommandlineoutputfromhandlesstructure
varargout{1}=handles.output;
%---Executesonbuttonpressinpushbutton1.
functionpushbutton1_Callback(hObject,eventdata,handles)
%hObjecthandletopushbutton1(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%---Executesonbuttonpressinpushbutton2.
functionpushbutton2_Callback(hObject,eventdata,handles)
%hObjecthandletopushbutton2(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%---Executesonbuttonpressinpushbutton3.
functionpushbutton3_Callback(hObject,eventdata,handles)
%hObjecthandletopushbutton3(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%---Executesonbuttonpressinpushbutton4.
functionpushbutton4_Callback(hObject,eventdata,handles)
%hObjecthandletopushbutton4(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%---Executesonbuttonpressinpushbutton5.
functionpushbutton5_Callback(hObject,eventdata,handles)
%hObjecthandletopushbutton5(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%---Executesonbuttonpressinpushbutton6.
functionpushbutton6_Callback(hObject,eventdata,handles)
%hObjecthandletopushbutton6(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%---Executesonbuttonpressinpushbutton7.
functionpushbutton7_Callback(hObject,eventdata,handles)
textString=get(handles.edit1,'String');
set(handles.edit1,'String','0');
%hObjecthandletopushbutton7(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%---Executesonbuttonpressinpushbutton26.
functionpushbutton26_Callback(hObject,eventdata,handles)
textString=get(handles.edit1,'String');
textString=strcat(textString,'7');
set(handles.edit1,'String',textString);
%hObjecthandletopushbutton26(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%---Executesonbuttonpressinpushbutton31.
functionpushbutton31_Callback(hObject,eventdata,handles)
textString=get(handles.edit1,'String');
textString=strcat(textString,'8');
set(handles.edit1,'String',textString);
%hObjecthandletopushbutton31(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%---Executesonbuttonpressinpushbutton32.
functionpushbutton32_Callback(hObject,eventdata,handles)
textString=get(handles.edit1,'String');
textString=strcat(textString,'9');
set(handles.edit1,'String',textString);
%hObjecthandletopushbutton32(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%---Executesonbuttonpressinpushbutton33.
functionpushbutton33_Callback(hObject,eventdata,handles)
textString=get(handles.edit1,'String');
textString=strcat(textString,'/');
set(handles.edit1,'String',textString);
%hObjecthandletopushbutton33(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%---Executesonbuttonpressinpushbutton34.
functionpushbutton34_Callback(hObject,eventdata,handles)
textString=get(handles.edit1,'String');
e=str2num(textString);
f=e^0.5;
set(handles.edit1,'String',f);
%hObjecthandletopushbutton34(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%---Executesonbuttonpressinpushbutton35.
functionpushbutton35_Callback(hObject,eventdata,handles)
textString=get(handles.edit1,'String');
textString=strcat(textString,'4');
set(handles.edit1,'String',textString);
%hObjecthandletopushbutton35(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%---Executesonbuttonpressinpushbutton36.
functionpushbutton36_Callback(hObject,eventdata,handles)
textString=get(handles.edit1,'String');
textString=strcat(textString,'5');
set(handles.edit1,'String',textString);
%hObjecthandletopushbutton36(seeGCBO)
%eventdatareserved-tobedefinedinafutureversionofMATLAB
%handlesstructurewithhandlesanduserdata(seeGUIDATA)
%---Executesonbuttonpressinpushbutton37.
functionpushbutton37_Callback
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Matlab 程序