




已阅读5页,还剩24页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
2.1set 与get 函数12.2callback函数32.3元胞数组42.4结构数组62.5矩阵操作102.6字符串操作132.7判断函数使用大全172.11打开外部程序212.11程序运行时间232.14动画242.12动画252.23显示多行内容272.24 uitable 使用272.27鼠标操作282.28键盘操作282.32粘贴板292.1set 与get 函数set(edit_handle,String,my value!); %String为Edit控件的属性 %2.1-1%创建figure对象hfig=figure(1); %创建坐标轴对象,指定其父对象为figure 1haxes1=axes(parent,hfig);prop.Color=b;prop.FontSize=12;set(haxes1,prop); %2.1-2%创建界面窗口hfig=figure(1); %查询其Units属性值get(hfig,units)%其Units属性值为pixels(像素)% ans=% pixels %2.1-3%figure的Pointer属性标识了鼠标指针的形状set(gcf,pointer);% 返回值为: crosshair | fullcrosshair | arrow | ibeam | watch | topl | topr | botl | botr | left | top | right | bottom | circle | cross | fleur | custom | hand %2.1-4%首先取得标识电脑屏幕大小的度量单位get(0,units) % ans =% pixels%取得屏幕的尺寸get(0,screensize) % ans =% 1 1 1280 800 2.2callback函数%定义M文件的主函数名称为DefineCallback,不带输入和输出参数function DefineCallback%创建界面窗口hFig= figure(units,normalize,. position,0.4 0.4 0.3 0.2);%在窗口中创建按钮控件,并定义其Callback属性uicontrol(parent,hFig,. style,pushbutton,. String,Execute Callback,. units,normalize,. position,0.4 0.4 0.3 0.2,. callback,figure;,. x = 0:pi/20:2*pi;,. y = sin(x);,. plot(x,y););%定义M文件的主函数名称为DefineCallback,不带输入和输出参数function DefineCallback%创建界面窗口hFig= figure(units,normalize,. position,0.4 0.4 0.3 0.2);%在窗口中创建按钮控件hpush=uicontrol(parent,hFig,. style,pushbutton,. String,Execute Callback,. units,normalize,. position,0.4 0.4 0.3 0.2);%设置按钮的Callback属性set(hpush,callback,mycallback); %定义回调函数为子函数function mycallback(hobj,event)figure;x = 0:pi/20:2*pi;y = sin(x);plot(x,y);2.3元胞数组a=hello 1 2 3;4 5 6;1 1 2a = hello 2x3 double 1 1x2 cell %示例2:将元胞数组a中的元胞逐一赋值 a1,1=hello;a1,2=1 2 3;4 5 6;a2,1=1;a2,2=1 2; aa = hello 2x3 double 1 1x2 cell %示例3:使用cell函数来创建元胞数组%生成2x3的元素为空数组的元胞数组 a=cell(2,3) a = %示例4:判断数组A是否为元胞数组%定义一个元胞数组A A=1 2 3;%判断A是否为元胞数组,如果为元胞数组,则函数 tf = iscell(A)tf = 1 %示例5:显示元胞数组C中的内容 clear C=Smith 1 2;3 4 12;%直接显示元胞数组C中的内容 celldisp(C) C1 = Smith C2 = 1 2 3 4C3 = 12%显示元胞数组C中的内容,数组的名称用cellcontent代替 celldisp(C,cellcontent) cellcontent1 = Smith cellcontent2 = 1 2 3 4 cellcontent3 = 12 %示例6:将字符数组转换为元胞数组 S = abc ; defg; hi m; cellstr(S)ans = abc %原先abc后面的空格被清除 defg hi m %i和m之间的空格仍然保留 %示例7:显示元胞数组S中的内容(包括空格和字符) S = abc , defg,hi m; cellplot(S) %示例8:将数字数组A按行或按列转换为元胞数组%A是4x3的数组 A=1 2 3;4 5 6;7 8 9;10 11 12; %把A的每一列转换为一个元胞,得到的C是13的元胞数组 C=num2cell(A,1)C = 4x1 double 4x1 double 4x1 double%把A的每一行转换为一个元胞,得到的C是41的元胞数组 C=num2cell(A,2)C = 1x3 double 1x3 double 1x3 double 1x3 double2.4结构数组 %示例1:使用直接法来创建结构数组 A(1).name = Pat; A(1).number = 176554;A(2).name = Tony;A(2).number = 901325; A A = 1x2 struct array with fields: name number %示例2:利用struct函数来创建结构数组 A(1)=struct(name,Pat,number,176554);A(2)=struct(name,Tony,number,901325); AA = 1x2 struct array with fields: name number %示例3:使用deal函数来得到结构体中各结构域的值%定义结构数组A A.name = Pat; A.number = 176554;A(2).name = Tony; A(2).number = 901325;%得到结构数组中所有name结构域的数据name1,name2 = deal(A(:).name) name1 =Patname2 =Tony%示例4:使用getfield函数来取得结构体中结构域的值%定义mystr结构数组 mystr(1,1).name = alice;mystr(1,1).ID = 0;mystr(2,1).name = gertrude; mystr(2,1).ID = 1;%取得mystr(2,1)的结构域name的值 f = getfield(mystr, 2,1, name)f =gertrude %示例5:删除结构数组中的指定结构域%定义结构数组s s.field1=1 2 3;s.field2=string;s.field3=1 2 3;4 5 6;%删除结构域field1 s=rmfield(s,field1) s = field2: string field3: 2x3 cell%删除结构域field2,field3 s=rmfield(s,field2,field3)s = field1: 1 2 3 %示例6:%定义结构数组s s.field1=1 2 3;s.field2=string;s.field3=1 2 3;4 5 6; ss = field1: 1 2 3 field2: string field3: 2x3 cell%将结构数组转换为元胞数组 c=struct2cell(s)c = 1x3 double string 2x3 cell %示例7:c = birch, betula, 65; maple, acer, 50c = birch betula 65 maple acer 50fields = name, genus, height; %fields包含struct中的结构域名s = cell2struct(c, fields, 2); %dim=2表示把c中的各行转换为struct数组s =2x1 struct array with fields:namegenusheight s(1)ans =name: birchgenus: betulaheight: 65 s(2)ans =name: maplegenus: acerheight: 50 fields = field1, field2; s = cell2struct(c, fields, 1); %dim=1表示把c中的各列转换为struct数组 s(1)ans =field1: birchfield2: maple s(2)ans =field1: betulafield2: acer s(3)ans =field1:65field2:50 2.5矩阵操作%示例1: find函数的使用方法。%定义矩阵A A=1 2 3 4;5 6 7 8; %查找A中所有大于3的元素,返回元素的线性索引 ind=find(A3)ind = 2 4 6 7 8%查找A中所有大于3的元素,返回元素的行下标和列下标 m n=find(A3)m = 2 2 2 1 2n = 1 2 3 4 4%示例2:矩阵元素的线性索引与行列下标之间的转换 A=1 2 3 4;5 6 7 8; ind=find(A3); m n=find(A3); I J=ind2sub(size(A),ind) IND=sub2ind(size(A),I,J)I = 2 2 2 1 2J = 1 2 3 4 4IND = 2 4 6 7 8%示例3:删除矩阵中的指定元素 A=1 2 3 4;5 6 7 8;%删除A的第1行的数据 A(1,:)= A = 5 6 7 8 A=1 2 3 4;5 6 7 8;%删除A的第1列的数据 A(:,1)=A = 2 3 4 6 7 8%示例4:查询矩阵的大小对于%由rand命令生成的一个432的三维矩阵 A=rand(4,3,2);%得到矩阵的大小信息 num=size(A)num = 4 3 2 num=length(A)num = 4 num=size(A,1)num = 4 num=size(A,2)num = 3 num=size(A,3)num = 2%示例5:求矩阵的最大和最小值 clear a=2 3;3 6;4 9a = 2 3 3 6 4 9 b=1 4;4 5;5 8b = 1 4 4 5 5 8 max(a)ans = 4 9 min(a)ans = 2 3 max(a,b)ans = 2 4 4 6 5 9 max(a,2)ans = 3 6 9 max(a,1)ans = 4 9 2.6字符串操作%示例1:创建字符串string%创建普通字符串 string=To study MATLAB!string =To study MATLAB!%创建带单引号的字符串,在出现单引号的地方用两个单引号代替() string=Were going to study MATLAB!string =Were going to study MATLAB! %示例2:将上述string字符串中的study替换为learn%创建字符串string string=Were going to study MATLAB!string =Were going to study MATLAB!%将其中的study替换为learn string(16:20)=learnstring =Were going to learn MATLAB! %示例3:取出string中的子串learn subString=string(16:20)subString =learn %示例4:将上述string字符串倒排 newString=string(end:-1:1)newString =!BALTAM nrael ot gniog ereW %示例5:计算字符串中字符的个数 r c=size(string)r = 1c = 28 %示例6:字符串的ASCII码值与字符串的转换%取得ASCII码值 ascii_string=double(string)ascii_string = Columns 1 through 21 87 101 39 114 101 32 103 111 105 110 103 32 116 111 32 108 101 97 114 110 32 Columns 22 through 28 77 65 84 76 65 66 33%转换为字符串 string=char(ascii_string)string =Were going to learn MATLAB! %示例7:字符串的比较和连接 str1=abcdefg;str2=hijklmn;str3=abckjhl;str4=ABCkjhl;%连接str1和str2为行向量 newstr1=strcat(str1,str2)newstr1 =abcdefghijklmn%连接str1、str2和str3为列向量 newstr2=strvcat(str1,str2,str3)newstr2 =abcdefghijklmnabckjhl%在newstr2中查找以abc开头的各行 strmatch(abc,newstr2)ans = 1 3%比较字符串str1和str4的前三个字符,区分字符的大小写 strncmp(str1,str4,3)ans = 0%比较字符串str1和str4的前三个字符,不区分字符的大小写 strncmpi(str1,str4,3)ans = 1 %示例8:数字数组和字符串的转换%创建数字数组A A=1 2 3;4 5 6;78 89 10A = 1 2 3 4 5 6 78 89 10%将数字数组A转换为字符数组 str=num2str(A)str = 1 2 3 4 5 678 89 10 whos str Name Size Bytes Class Attributes str 3x10 60 char%将字符数组str转换为数字数组 num=str2num(str)num = 1 2 3 4 5 6 78 89 10 whos num Name Size Bytes Class Attributes num 3x3 72 double %将数字数组A转换为字符串(行向量),向量中的元素包括“”、“”、“;”等字符。 str2=mat2str(A)str2 =1 2 3;4 5 6;78 89 10 whos str2 Name Size Bytes Class Attributes str2 1x22 44 char %示例9:%字符串中字符的大小写的转换 str=matlabstr =matlab str1=upper(str)str1 =MATLAB str=lower(str1)str =matlab%查找在str1中出现str2的位置 str1=abcdefg;str2=cdf; findstr(str1,str2)ans = str1=abcdefg;str2=cd; findstr(str1,str2)ans = 3 2.7判断函数使用大全 %示例1h=gcf;value=rand(3);setappdata(h,myappdata,value);tf=isappdata(h,myappdata)%示例2:A1,1 = 1 4 3; 0 5 8; 7 2 9;A1,2 = Anne Smith;A2,1 = 3+7i;A2,2 = -pi:pi/10:pi;iscell(A)%示例3:A1,1 = Thomas Lee;A1,2 = Marketing;A2,1 = Allison Jones;A2,2 = Development;iscellstr(A)%示例4:% 双精度数字数组C1,1 = magic(3); %字符数组C1,2 = John Doe;%复数数组C1,3 = 2 + 4i;% C% C = % 3x3 double John Doe 2.0000 + 4.0000i%ischar函数返回值表明只有C1,2是字符数组for k = 1:3x(k) = ischar(C1,k);end% x% x =% 0 1 0%示例5:tf=isdir(D:work)%示例6A=rand(2,3);B=;tf1=isempty(A)tf2=isempty(B)%示例7A=1 2 3;4 5 6;B=7 8 9;10 11 12;tf1=isequal(A,B);%示例8 = John Doe;patient.billing = 127.00;isfield(patient,billing)%示例9a = -2 -1 0 1 2;isfinite(1./a)isfinite(0./a)%示例10h=gcf;tf1=ishandle(h)close alltf2=ishandle(h)%示例11global AA=100;isglobal(A)%示例12 = John Doe;patient.billing = 127.00;isstruct(patient)%示例13C1,1 = pi;C1,2 = John Doe;tf1=isnumeric(C1,1)tf2=isnumeric(1,2)%示例14C1,1 = ispc;C1,2 = John Doe;tf1=islogical(C1,1)tf2=islogical(1,2)%示例15set = 0 2 4 6 8 10 12 14 16 18 20;A=1;2;3;4;5;ismember(A, set)%示例16a = -2 -1 0 1 2;isnan(0./a)%示例17x = magic(3);y = complex(x);isreal(x)isreal(y)%示例18A = rand(5);isscalar(A)isscalar(A(3,2)%示例19A = 5 12 33 39 78 90 95 107 128 131;issorted(A)%示例20s1 = serial(COM1);s2 = serial(COM1);delete(s2)sarray = s1 s2;isvalid(sarray)2.11打开外部程序% 示例1:open copyfile.m % 示例2:open(D:worksdata.mat) %示例3open myfile % 示例4 clear a=10; b=20; c=rand(5) d.value1=10; d.value2=20; d.value3=30; saveSaving to: matlab.mat returnValue=open(matlab.mat)returnValue = a: 10 b: 20 c: 5x5 double d: 1x1 struct value1=returnValue.dvalue1 = value1: 10 value2: 20 value3: 30 % 示例5:open(myfile.doc); % 示例6:open(myfile.xls); % 示例7:!myfile.doc % 示例8:!start notepad.exe 1.txt !start winword.exe 1.doc !start excel.exe 1.xls !start powerpnt.exe 1.ppt % 示例9:winopen(myfile.doc); % 示例10: winopen(D:workmyresults.html); web -browser /thread-121441-1-2.html 2.11程序运行时间% 示例1:使用tic和toc来计算程序运行的时间tic; figure(1);surf(peaks(40); t=toc; disp(t); % 示例2:clock命令的使用方法 t1=clockt1 = Columns 1 through 4 2010 8 25 23 Columns 5 through 6 28 36.359%示例3: fix(t1)ans = 2010 8 25 23 28 36% %示例4t1=clock;figure(1);surf(peaks(40); t2=clock;t=etime(t2,t1);disp(程序运行时间为:,num2str(t),秒); % 示例5:使用cputime命令得到程序运行时间t1 = cputime;figure(1);surf(peaks(40);t= cputime-t1;2.14动画% 示例1theta = 0:0.001:2*pi;r = 10;x=r*cos(theta);y=r*sin(theta);set(gcf,closerequestfcn,);comet(x,y);set(gcf,closerequestfcn,closereq); % 示例2t = -10*pi:pi/250:10*pi;set(gcf,closerequestfcn,);comet3(cos(2*t).2).*sin(t),(sin(2*t).2).*cos(t),t);set(gcf,closerequestfcn,closereq);%示例3clc;clear;figure(1);x=0:pi/50:2*pi;y=sin(x);h=plot(x,y);axesValue=axis;set(gcf,closerequestfcn,);for ii=1:10 for jj=1:length(x)*ii/10 set(h,xdata,x(1:jj),ydata,y(1:jj),color,r); axis(axesValue); end A(ii)=getframe;endmovie(A);set(gcf,closerequestfcn,closereq);%示例4figure(1);x=0:pi/50:2*pi;y=sin(x);plot(x,y);% 产生一个红点,在曲线上移动h=line(0,0,color,r,marker,.,markersize,40,erasemode,xor);%保存坐标值,使得所有的帧都在同一个坐标系中axesValue=axis;%创建动画%画出每一步的曲线set(gcf,closerequestfcn,)for jj=1:length(x) set(h,xdata,x(jj),ydata,y(jj),color,r); axis(axesValue); pause(0.2); drawnow;endset(gcf,closerequestfcn,closereq)web -browser /thread-121450-1-2.html 2.12动画% 示例1theta = 0:0.001:2*pi;r = 10;x=r*cos(theta);y=r*sin(theta);set(gcf,closerequestfcn,);comet(x,y);set(gcf,closerequestfcn,closereq); % 示例2t = -10*pi:pi/250:10*pi;set(gcf,closerequestfcn,);comet3(cos(2*t).2).*sin(t),(sin(2*t).2).*cos(t),t);set(gcf,closerequestfcn,closereq);%示例3clc;clear;figure(1);x=0:pi/50:2*pi;y=sin(x);h=plot(x,y);axesValue=axis;set(gcf,closerequestfcn,);for ii=1:10 for jj=1:length(x)*ii/10 set(h,xdata,x(1:jj),ydata,y(1:jj),color,r); axis(axesValue); end A(ii)=getframe;endmovie(A);set(gcf,closerequestfcn,closereq);%示例4figure(1);x=0:pi/50:2*pi;y=sin(x);plot(x,y);% 产生一个红点,在曲线上移动h=line(0,0,color,r,marker,.,markersize,40,erasemode,xor);%保存坐标值,使得所有的帧都在同一个坐标系中axesValue=axis;%创建动画%画出每一步的曲线set(gcf,closerequestfcn,)for jj=1:length(x) set(h,xdata,x(jj),ydata,y(jj),color,r); axis(axesValue); pause(0.2); drawnow;endset(gcf,closerequestfcn,closereq)2.23显示多行内容figure(units,normalized,. position,0.4 0.4 0.4 0.3);h = uicontrol(Style,Text,fontsize,16);string = 静态文本框为什么是静态的?,. 因为不能像编辑框一样滚动显示其中的内容,. 如果想在静态文本框中多行显示,. 按照这种方式就可以实现,. 显示之前调用textwrap函数!;outstring,newpos = textwrap(h,string);set(h,String,outstring,Position,newpos);2.24 uitable 使用 f = figure(units,normalized,. Pos
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025-2030中国深海作业抗高压功能性饮品研发报告
- 2025-2030中国植物提取物出口竞争力与贸易壁垒报告
- 酒店餐饮经营管理制度
- 租赁电动车管理规定
- 2025年初中学业水平考试地理模拟卷及答案:地理环境与可持续发展模拟试题型
- 2025年营养师基础知识考核试卷:营养师项目管理与团队领导试题
- 2025年初中学业水平考试地理模拟卷及答案(乡土地理特色试题)
- 2025年健身教练职业技能考核试卷:健身教练沟通与客户服务试题
- 杭州治疗类器械股份有限公司2024下半年绩效报告
- 企业名称环境、社会与公司治理报告2025下半年绩效报告监护类器械
- 管理咨询项目考核方案
- Unit 1~2单元月考测试(含答案) 2025-2026学年译林版(2024)八年级英语上册
- 2025年五粮液笔试考试题及答案
- 第49部分:碳酸根、重碳酸根和氢氧根离子的测定 滴定法(报批稿)
- T/CAAM 0004-2023针刺临床试验中假针刺对照设置与报告指南
- 立陶宛语儿童文学的语言特点论文
- 民宿的内涵专题课件
- 高一必修一英语单词默写表
- 人教版六年级数学上册第一单元测试卷
- 2024年注册安全工程师生产技术押密试题及答案
- 高标准农田设计实施方案(技术标)
评论
0/150
提交评论