1、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=
2、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 | w
3、atch | 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(un
4、its,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%创
5、建界面窗口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;
6、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是否为元胞数组,如果为元胞数组,则函数 t
7、f = iscell(A)tf =1%示例5:显示元胞数组C中的内容 clear C=Smith 1 2;3 4 12;%直接显示元胞数组C中的内容 celldisp(C) C1 = Smith C2 = 1 23 4C3 = 12%显示元胞数组C中的内容,数组的名称用cellcontent代替 celldisp(C,cellcontent) cellcontent1 = Smith cellcontent2 = 1 23 4 cellcontent3 = 12%示例6:将字符数组转换为元胞数组 S = abc ; defg; hi m; cellstr(S)ans = abc %原先abc后
8、面的空格被清除defghi 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 double1x3 double1x3
9、double1x3 double2.4 结构数组%示例1:使用直接法来创建结构数组 A(1).name = Pat; A(1).number = 176554;A(2).name = Tony;A(2).number = 901325; AA = 1x2 struct array with fields:namenumber%示例2:利用struct函数来创建结构数组 A(1)=struct(name,Pat,number,176554);A(2)=struct(name,Tony,number,901325); AA = 1x2 struct array with fields:namenu
10、mber%示例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
11、,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: stringfield3: 2x3 cell%删除结构域field2,field3 s=rmfield(s,field2,field3)s = field1: 1 2 3%示例6:%定义结构数组s s
12、.field1=1 2 3;s.field2=string;s.field3=1 2 3;4 5 6; ss = field1: 1 2 3field2: stringfield3: 2x3 cell%将结构数组转换为元胞数组 c=struct2cell(s)c = 1x3 doublestring2x3 cell %示例7:c = birch, betula, 65; maple, acer, 50c = birch betula 65maple acer 50fields = name, genus, height; %fields包含struct中的结构域名s = cell2struct
13、(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
14、 =field1: betulafield2: acer s(3)ans =field1:65field2:502.5 矩阵操作%示例1: find函数的使用方法。%定义矩阵A A=1 2 3 4;5 6 7 8; %查找A中所有大于3的元素,返回元素的线性索引 ind=find(A3)ind =24678%查找A中所有大于3的元素,返回元素的行下标和列下标 m n=find(A3)m =22212n =12344%示例2:矩阵元素的线性索引与行列下标之间的转换 A=1 2 3 4;5 6 7 8; ind=find(A3); m n=find(A3); I J=ind2sub(size(A)
15、,ind) IND=sub2ind(size(A),I,J)I =22212J =12344IND =24678%示例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 46 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=si
16、ze(A,2)num =3 num=size(A,3)num =2%示例5:求矩阵的最大和最小值 clear a=2 3;3 6;4 9a =2 33 64 9 b=1 4;4 5;5 8b =1 44 55 8 max(a)ans =4 9 min(a)ans =2 3 max(a,b)ans =2 44 65 9 max(a,2)ans =369 max(a,1)ans =4 92.6 字符串操作%示例 1:创 建字符串 string%创建普通字符串 string=To study MATLAB!string =To study MATLAB!%创建带单 引号的字符串,在出现单引号的地方用
17、两个单引号代替() 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 中的子串 lea
18、rn 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 2187 101 39 114 101 32 103 11
19、1 105 110 103 32 116 111 32 108 101 97 114 110 32Columns 22 through 2877 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%连接
20、str1、str2 和 str3 为列向量 newstr2=strvcat(str1,str2,str3)newstr2 =abcdefghijklmnabckjhl%在 newstr2 中查找以abc 开头的各行 strmatch(abc,newstr2)ans =13%比较字符串 str1 和 str4 的前三个字符,区分字符的大小写 strncmp(str1,str4,3)ans =0%比较字符串 str1 和 str4 的前三个字符,不区分字符的大小写 strncmpi(str1,str4,3)ans =1%示例 8:数字数 组和字符串的转换%创建数字数 组 A A=1 2 3;4 5
21、 6;78 89 10A =1 2 34 5 678 89 10%将数字数组 A 转换为 字符数组 str=num2str(A)str =1 2 34 5 678 89 10 whos strName Size Bytes Class Attributesstr 3x10 60 char%将字符数组 str 转换为数字数组 num=str2num(str)num =1 2 34 5 678 89 10 whos numName Size Bytes Class Attributesnum 3x3 72 double %将数字数组 A 转换为 字符串(行向量),向量中的元素包括“” 、“”、“;
22、”等字符。 str2=mat2str(A)str2 =1 2 3;4 5 6;78 89 10 whos str2Name Size Bytes Class Attributesstr2 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; find
23、str(str1,str2)ans =32.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)%示
24、例 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;
25、tf1=isequal(A,B);%示例 8patient.name = 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)%示例 12patient.name = John Doe;patient.billing = 127.00;isstruct(patient)%示
26、例 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 =
27、 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=
28、20; d.value3=30; saveSaving to: matlab.mat returnValue=open(matlab.mat)returnValue = a: 10b: 20c: 5x5 doubled: 1x1 struct value1=returnValue.dvalue1 = value1: 10value2: 20value3: 30% 示例5:open(myfile.doc); % 示例6:open(myfile.xls);% 示例7:!myfile.doc% 示例8:!start notepad.exe 1.txt !start winword.exe 1.doc
29、 !start excel.exe 1.xls !start powerpnt.exe 1.ppt% 示例9:winopen(myfile.doc);% 示例10:winopen(D:workmyresults.html);web -browser http:/ 程序运行时间% 示例1:使用tic和toc来计算程序运行的时间tic; figure(1);surf(peaks(40); t=toc; disp(t);% 示例2:clock命令的使用方法 t1=clockt1 =Columns 1 through 42010 8 25 23Columns 5 through 628 36.359%
30、示例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,
31、);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:10f
32、or jj=1:length(x)*ii/10set(h,xdata,x(1:jj),ydata,y(1:jj),color,r);axis(axesValue);endA(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=ax
33、is;%创建动画%画出每一步的曲线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 http:/ 动 画% 示例 1theta = 0:0.001:2*pi;r = 10;x=r*cos(theta);y=r*sin(theta);set(gcf,closerequestfcn,);comet(x,y);set(gc
34、f,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:10for jj=1:length(x)
35、*ii/10set(h,xdata,x(1:jj),ydata,y(1:jj),color,r);axis(axesValue);endA(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;%创建动画%画出每一
36、步的曲线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 = 静态文本框为什么是静态的? ,.因为不能像编辑框一样滚动显示其中的
37、内容,.如果想在静态文本框中多行显示, .按照这种方式就可以实现, .显示之前调用 textwrap 函数!;outstring,newpos = textwrap(h,string);set(h,String,outstring,Position,newpos);2.24 uitable 使用f = figure(units,normalized,.Position,0.4 0.4 0.6 0.4);columnname = 姓名, 性别, 年龄, 职称, 在职 ,备注;columnformat = char, char,numeric,char,logical, 1 0; columned
38、itable = true false true false false true; data =张三 男 23 研究实习员 true 1; .李四 女 32 助理研究员 false 0;.王二 男 45 副研究员 true 1;.孙总 男 51 研究员 false 0;t = uitable(units,normalized,.Position,0.1 0.1 0.8 0.8,.Data, data,.ColumnName, columnname,.ColumnFormat, columnformat,.ColumnEditable, columneditable);2.27 鼠 标操作if
39、 strcmp(get(gcf,SelectionType),normal)msgbox(按下了鼠标左键);elseif strcmp(get(gcf,SelectionType),alt)msgbox(按下了鼠标右键);elseif strcmp(get(gcf,SelectionType),extend)msgbox(按下了鼠标中间键);elseif strcmp(get(gcf,SelectionType),open)msgbox(鼠标双击操作);end2.28 键盘 操作%示例 1function keyboardinput()%设置 figure 的 KeyPressFcn 回调函数
40、,以响应键盘按下的事件figure(KeyPressFcn,keypress);function keypress(hobj,event)%取得 figure 的 CurrentCharacter 属性 值,并在命令窗口显示key=get(hobj,CurrentCharacter);disp(key);endend%示例 2function keyboardinputfigure(KeyPressFcn,keypress);function keypress(hobj,event)keyChar=event.Character;keyModifier=event.Modifier;key=event.Key;disp(keyChar);disp(keyModifier);disp(key);endend2.32 粘 贴板%示例1data=将字符串内容复制到粘贴板;str=mat2str(data);clipboard(copy, data);str1 = clipboard(paste);%示例2hf=figure(1);plot(peaks);hgexport(hf,-clipboard);