matlab常用且全面的基本函数

本文详细介绍MATLAB中的常用编程技巧,包括数学运算、图形绘制、多项式处理、数据插值及拟合等内容,适合初学者及有一定经验的使用者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个m文件,测试了很多常用的代码,应该很全面了。当时是按顺序测试的代码,每一段小程序都有分隔和简短的说明,所以会有%注释,需要复制代码片运行。(ps:这里可以先Ctrl+F查找下是否有你需要的东西)。


clc,clear;  close('all')

%  a·x=b    =>  x = inv(a)*b

% a=[1,2,3;4,5,6;7,8,0];
% b=[366;804;351];
% det(a)
% x=inv(a)*b
% 
% disp('a*x =' ),disp(a*x)
% 
% disp('a\b ='),disp(a\b) 
% 
% a*(a\b)
% 
% [v d]=eig(a)
% orth(v)
% [s v d]=svd(a)

%%  时间
%  t1=tic
% for n=1:5
%     for m=6:-1:1
%      x(n,m)=n+m;
%     end
%     disp(n);
% end 
%  t=toc(t1)   %t1的时间

% t1=clock; % 【年 月 日 时 分 秒】
% for n=1:10
%     x(n)=sin(n);
% end
% t2=clock;
% etime(t2,t1)

% t1=cputime;
% for n=1:10
%     x(n)=sin(n);
% end
% t2=cputime;
% t=t2-t1+1

%%
% %eps
% i=51;a=0.5
% while i>0
% a=a/2;i=i-1;
% end
% a

% a=12;
% if a<2
%     disp('1')
% elseif a<8
%     disp('2')
% elseif a<12
%     disp('3')
% else
%     disp('4')
% end

% a=10,b=7;
% if a==10
%     c=a+b
%     if b==7
%         error('c');
%     end
% end

% %% 
% y=ones(6,3);
% ad=[-1,0,1];
% y=y + ad(ones(1,size(y,1)),:) + 0.2*randn(6,3);
% x=1:6;
% avg = mean(y);
% 
% % min(min(y));  %全局最小
% % [m,i]=min(y)  % i为各列的最小值所在 行,是一个行向量
% % [m,ic]=min(m)   % ic为 上一句中m的最小值的索引,即 列
% % [i(ic) ic] % 坐标
% 
% %tic; 
% % for i=1:3,dev(:,i)=y(:,i)-avg(i);  % 矩阵和标量运算  0.000176 
% % for i=1:3,dev(i,:)=y(i,:)-avg;     % 矩阵和矩阵运算  0.000184
% % end ; toc;
% 
% tic; dev = y - avg(ones(1,6),:); toc;   %先复制为相同大小 再作减  0.000065
% 
% [max_y idx_max]= max(y,[],1);    %  等效 max(y)  列     max(y,[],2) 行最大
% [min_y idx_min]= min(y,[],1);
% 
% y_dev = std(y);
% y_diff = diff(y)  ; % y_diff(i)= y(i)-y(i-1)
% 
% plot(x,y)

%% 累计和,累计积 
% y=ones(6,3);
% ad=[-1,0,1];
% y=y + ad(ones(1,size(y,1)),:)%+ 0.2*randn(6,3)
% 
% covmatrix = cov(y);
% corceorf = corrcoef(y) ;%  cov2corr(y)  必须是方阵
% cumprod(y);     %列累计积
% cumsum(y);      %列累计和
% diff(y,2);    %  =diff(diff(y))
% 
% gradient(y);
% prod(y);

%% 多项式
% r=roots([1 -12 0 25 116])       % 多项式系数(行向量)求根(列向量) 
% pp=poly(r)                      % 根(列向量) 构造多项式(系数,行向量)

%a=[1  2  3  4] ;        % a(x)=x3+2x2+3x+4
%b=[1  4  9  16];        % b(x)= x3+4x2+9x+16
%c=conv(a , b)           % conv支持多项式乘法(执行两个数组的卷积)
                        % c(x)=x6+6x5+20x4+50x3+75x2+84x+64
% d=a+b
% g = c+[0 0 0 d]        % 两个多项式阶次不同,低阶的多项式必须用首零填补(矩阵相加,大小必须相同)

%[q , r]=deconv(c , b)  % 多项式除法  q是商, r是余数
%h=polyder(c)            % 微分,系数   h=c'(x) = 6x5 + 30x4 + 80x3 + 150x + 84 

% tic
% x=linspace(-1, 3);  % 变量范围
% p=[1  4  -7  -10];
% v=polyval(p , x) ;    % v=x.^3+4*x.^2-7*x-10; 
% plot(x , v, ['b','+', '-.']),title(' x^3+4x^2-7x-10 ');
% xlabel(' x '), ylabel(' y ')
% toc

% num=10*[1  2] ;                     % 分子多项式
% den=poly([-1;  -3;  -4]) ;          % 分母多项式 
% [res, poles, k]=residue(num, den)
% [n, d]=residue(res, poles, k)       % n是分子多项式系数, d是分母多项式系数
% 
% [a , b]=polyder(num , den)          % 多项式求导  a是结果分子多项式求导 
%                                     %             b是结果分母多项式求导

%% 曲线拟合
% x=[0  .1  .2  .3  .4  .5  .6  .7   .8  .9  1];
% y=[-.447  1.978  3.28  6.16  7.08  7.34  7.66  9.56  9.48  9.30  11.2];
%                             % plot(x,y) 就是原始数据
% n=9;                        % 拟合曲线的阶数
% p=polyfit(x, y, n);         % 拟合多项式的系数
% format shorte
% xi=linspace(0, 1, 100);
% z=polyval(p, xi);           % 计算在xi范围对应的多项式离散值
% 
% plot(x, y, ' o ' ,x,y , xi, z, ' : ' )  %  三条曲线   
%                                         %  x, y, ' o '     散点
%                                         %  x,y           连线(在一图基础上)
%                                         %  xi, z, ' : '   第三条(拟合结果)
% xlabel(' x '),  ylabel(' y=f(x) '),  title(' 2nd and 10th Order curve Fitting ')

%% 插值       
% % 在数据点之间用60个点,它比另一个只用6个点更光滑和更精确
% x1=linspace(0, 2*pi, 60);
% x2=linspace(0, 2*pi, 6);
% plot(x1, sin(x1), x2, sin(x2), ' - ')  %线条和标签颜色不一样
% xlabel(' x '),  ylabel(' sin(x) '),  title(' Linear Interpolation ')

% 一维插值 interp1  ,方法有 最近邻,线性,3次(样条插值)...
% hours=1:12;
% temps=[5  8  9  15  25  29  31  30  22  25  27  24];
% plot(hours, temps, hours, temps,' + ')   %散点图,再用实线依次连接
% title(' Temperature '), xlabel(' Hour '),  ylabel(' Degrees Celsius ')
% hold on
% 
% t=interp1(hours, temps, 9.3)  %预测9.3h时的温度值t
% t=interp1(hours, temps, [3.2  6.5  7.1  11.7])
% 
% % 样条插值
% t=interp1(hours, temps, 9.3, 'spline')
% t=interp1(hours, temps, [3.2  6.5  7.1  11.7],'spline') 

% h=1:0.1:12;
% t=interp1(hours, temps, h, 'spline') ;      %对时间取值集合h求样条插值集合t
% plot( hours, temps,[ '+',':'] , h, t)       %线条和标签颜色一样

% 二维插值 interp2
% width=1:5;             %  index for width of plate (i.e.,the x-dimension)
% depth=1:3;             %  index for depth of plate (i,e,,the y-dimension)
% temps=[82  81  80  82  84; 79  63  61  65  81; 84  84  82  85  86];
% figure(1),mesh(width, depth, temps)
% 
% % figure(2)
% % wi=1:0.2:5;d=2;
% % zlinear=interp2(width, depth, temps, wi, d) ;
% % zcubic=interp2(width, depth, temps, wi,d, 'cubic') ;
% % plot(wi, zlinear, ' - ' , wi, zcubic,': ')
% % xlabel(' Width of Plate '),  ylabel(' Degrees Celsius ')
% % title( [' Temperature at Depth =  '  num2str(d) ] )
% 
% figure(3)
% mesh(width, depth, temps)
% xlabel(' Width of Plate '),  ylabel(' Depth of Plate ')
% zlabel(' Degrees Celsius '),  axis('ij'),  grid
% 
% wi=1:0.2:5;     %  choose higher resolution for width
% di=1:0.2:3;   %  choose higher resolution for depth
% zcubic=interp2(width, depth, temps, wi,di, 'cubic') ;   %  cubic
% mesh(wi, di, zcubic)
% xlabel(' Width of Plate '),  ylabel(' Depth of Plate ')
% zlabel(' Degrees Celsius '),  axis('ij'),  grid

%三次样条
% x=0 : 12;
% y=tan(pi*x/25);
% xi=linspace(0, 12);
% yi=spline(x, y, xi);
% plot(x, y, ' o ', xi, yi),    % x,y散点, 用样条函数进行拟合
% title(' Spline fit ')

% hold on
% pp=spline(x, y); % 包含了对于任意一组所期望的内插值和计算三次样条所必须的全部信息
% yi=ppval(pp, xi); % xi区间的插值结果
% plot(xi, yi, ' - '),
% hold on

% xi2=linspace(10, 12);
% yi2=ppval(pp, xi2);     % xi2区间的插值结果
% plot(xi2, yi2, ' + '),

% hold on
% xi3=10 : 15;            % xi2区间的插值结果
% yi3=ppval(pp, xi3)
% plot(xi3, yi3, ' * '),

%% 数值分析
% 绘图
% fplot(' humps ' , [0  2])
% title(' FPLOT OF HUMPS ')
%
% fplot('2*exp(-x) .* sin(x)' , [0  8]);   % 或者定义 f='2*exp(-x) .* sin(x)';
% title(f) , xlabel('x')                  % 再用 fplot(f , [0  8]);
%与上一个类似
% x=0:0.01:8; f=2*exp(-x) .* sin(x);  plot(x,f)

% 零点
% xzero=fzero('humps' , 1.2)    % humps曲线在1.2附近的零点
% yzero=humps(xzero)             % 验证曲线在xzero是否为0
%
% f='2*exp(-x) .* sin(x)-1';
% fplot(f , [-4  8]);
% xzero=fzero(f , 1.2)
% %或者
% xzero=fzero('2*exp(-x) .* sin(x)-1' , 1.2)


% 积分   trap2 , quad和quad8
% x=-1 : 0.17 : 2; 
% %x=-1 : 0.07 : 2;   % 区间细致,结果越准确
% y=humps(x);
% area=trapz(x , y)  % 用trapz在区间-1<x<2上计算y=humps(x)下面的面积
%
%area=quad('humps' , -1 , 2)

% 微分
% x=[0  .1  .2  .3  .4  .5  .6  .7  .8  .9  1]
% y=[-.447  1.978  3.28  6.16  7.08  7.34  7.66  9.56  9.48  9.30  11.2];  % data
% n=2;  %  order of fit
% p=polyfit(x , y , n)   %  find polynomial coefficients (这里是一个二次多项式)
% xi=linspace(0 , 1 , 100);
% z=polyval(p , xi);    %  evaluate polynomial
% subplot(1,2,1),plot(x , y , ' o ' , x , y , xi , z , ' : ')
% xlabel(' x ') , ylabel(' y=f(x) ') , title(' Second Order Curve Fitting ')
% 
% pd=polyder(p)  %求微分,微分后多项式的系数  (二次微分,为直线)
% z=polyval(pd , xi);  %  evaluate derivative
% plot(xi , z)
% 
% dy=diff(y) ./ diff(x);
% xd=x(1 : length(x)-1);
% subplot(1,2,2),plot(xd , dy);
% title(' Approximate Derivative Using DIFF '),
% ylabel(' dy/dx ') , xlabel(' x ')

%% 二维图形
% x=linspace(0,2*pi,20);
% y=sin(x);z=cos(x);
% %plot(x,y,x,z)  %同时画在一个曲线中   或者 w=[y; z],plot(x,w)  行对应曲线
% %
% % w=[y',z'];   
% % plot(x,w)     % w是矩阵,按照列进行画图,长度为y或z的长度
% %
% plot(x,y,'b.:',x,z,'*r-.')   % 线型、颜色、标记可以任意组合放在一个字符串''中
% grid %栅格
% xlabel(' x '),ylabel('y and z '), title ('sin and cos cruves')
% %
% % text(2.5,0.7,'sin(x)','FontSize',18);
% % gtext('cos(x)')                         %手动选择一个位置,放置指定文本标签
% %
% legend('sin(x)','cos(x)')      % 生成图例
% %legend off  % 关闭图例
% % 
% %  axis name 同 asix('name')
% %
% %axis auto                  % 自动,等效axis('auto'),和不加该句时结果一样 
% %axis([0 2*pi -1 1])        % 限定2个坐标抽显示范围
% %axis xy                    % 缺省,笛卡尔坐标, 也就是 xoy 原点左下角,y值从左至右增加
% %axis ij                    % 原点左上角, y值从上到下增加
% %axis square                % 方形    square
% %axis equal                 % 定标因子相同
% %axis image                 % 像素
% axis([0 2*pi -1 1], 'xy' )   % 可以多个同时设置
% %
% % 显示控制
% % h1=figure;plot(x,y);   % 新的图像窗口,并制定句柄为h1
% % close(h1)             % close(h1), 关闭句柄为h1的句柄
% %
% % figure
% % % subplot(m,n,p)       % 子图,一个图分为m*n个子图,p为第几个
% % subplot(1,2,1); plot(x,y)
% % subplot(1,2,2); plot(x,z)
% % 
% % clf     %  清除当前窗口   
% %         %  clf reset 清楚当前窗口,并重设所有特征,asix,label,legend...
%         
% % [i, j, button]=ginput();    % 从窗口用十字线获取点,(i,j)是选择的坐标, 
% %                             % 可以设置N个或不限制个点,enter结束
% %                             % button是鼠标或者键盘获取点的方式代码
% 
% x=[0 1 1 0 ],y=[ 0 0 1 1];
% fill(x,y,'y');              % 黄色填充 x,y序列点包围的区域
% axis([-1 2 -1 2]);

% 特殊图像 
% theta = linspace(0,2*pi);
% rho=sin(2*theta).*cos(2*theta);
% polar(theta,rho,'b');             % 极坐标
% title('polor');
% 
% x=linspace(-2.5,2.5,20)
% y=exp(-x.*x);
% bar(x,y)                    % 条状
% hold, stairs(x,y,'r')       % 阶梯  
% 
% x=linspace(-2.5,2.5,20);
% y=randn(5000,1);
% hist(y,x)                         % 直方图
% t=randn(1000,1)*pi;    rose(t)    % 玫瑰图
% y=randn(50,1); stem(y, 'gs-.')    % 离散序列  属性参数同plot
% x=0:0.1:2;y=erf(x);
% e=0.1*rand(size(x));
% errorbar(x,y,e)                   % 误差条形图
% x=randn(20,20),z=eig(x),compass(z)  % 罗盘图
% x=randn(20,20),z=eig(x),feather(z)  % 羽毛图
% % load clown;                         % 加载图像,X为数据,map为调色板
% % image(X),colormap(map),axis off     % 显示图像,配置调色板,关闭坐标轴


%% 三维图
% t=0:pi/50:10*pi; plot3(sin(t),cos(t),t)
% title('Helix'),xlabel('sint(t)'),ylabel('cos(t)'),zlabel('t')
% text(0,0,0,'Origin'), grid

% x=linspace(0,3*pi);
% y1=zeros(size(x)); y2=ones(size(x)); y3=2*ones(size(x));
% z1=sin(x); z2=sin(2*x); z3=sin(3*x);
% plot3(x,y1,z1,x,y2,z2,x,y3,z3), 
% grid,xlabel('x-axis'),ylabel('y-axis'),zlabel('z-axis')
% [az,el]=view    % 视图角

% x=-3:0.8:3;  %  choose x-axisd values
% y=-2:0.8:2;  %  y-axis values
% [X,Y]=meshgrid(x,y);
% z = exp(-X.^2-Y.^2);
% mesh(x,y,z),size(z)             % 作图 mesh(z)同样
% xi=-3:0.5:3;  yi=-2:0.5:2; 
% [Xi,Yi]=meshgrid(xi,yi);
% zi=griddata(x,y,z,Xi,Yi);       % 精度提高,并且插值
% figure,mesh(zi),size(zi)

% figure(1),
% [X,Y,Z]=peaks(80);
% mesh(X,Y,Z);       %网格图
% grid,xlabel('x-axis' ),ylabel('y-axis'),zlabel( 'z-axis' )
% title('MESH of PEAKS '), grid
% hidden off   % 透明效果
% 
% figure(2)
% [X,Y,Z]=peaks(80);
% surf(X,Y,Z)     % 曲面图
% xlabel('x-axis' ),ylabel('y-axis'),zlabel( 'z-axis' )
% title('SURF of PEAKS '),grid
% % shading flat    % 平滑颜色快
% %shading interp    % 插值平滑
% colormap pink     % 配色方案

% [X,Y,Z]=peaks(50);
% x=X(1,:); y=Y(:,1); 
% i=find(y>.8 & y<1.2); j=find(x>-.6 & x<.5);   %设置想x,y指定区域为空值nan
% Z(i,j)=nan*Z(i,j);
% surf(X,Y,Z)               % 作图函数忽略 nan值的点
% % pcolor(X,Y,Z)         % 二维下的伪色彩

% 还有 surfc,surfz, surfl, ...
% [X,Y,Z]=peaks(50);  
% pcolor(X,Y,Z),shading interp, hold on    % 二维下的伪色彩
% contour3(X,Y,Z,19,'k')                   % 等值线图(等高线)
% grid,xlabel('x-axis'),
% ylabel('y-axis'),zlabel('z-axis')
% title('CONTOUR3 of PEAKS')
% % N=16, clf,view(3),hold on
% % set(gca,'ColorOrder',hsv(N));    % 使用colormap
% % contour3(X,Y,Z,N) ,hold off    

% [X,Y,Z]=peaks(30);
% waterfall(X,Y,Z)
% xlabel('X-axis'),ylabel('Y-axis'),zlabel('Z-axis')   

% [X,Y,Z]=peaks(16);
% [DX,DY]=gradient(Z, .5, .5);    % 梯度
% contour3(X,Y,Z,10)              % 等值线图
% hold on, quiver(X,Y,DX,DY)      % 梯度变化图

% fill3(rand(4,5),rand(4,5),rand(4,5),'y')    %3维填充   一列(一次连接)为一个图像

% [X,Y,Z]=peaks(30);
% [cs,h]=contour(X,Y,Z,8);     %  等值线
% clabel(cs);         %  等值线输出值
% xlabel('X-axis'),ylabel('Y-axis')
% title('CONTOUR  of  PEAKS  with  LABELS')

% 动画
% [X,Y,Z]=peaks(30);          % 图像数据
% surfl(X,Y,Z);                % 图像
% axis([-3,3,-3,3,-10,10])    % 坐标显示范围
% axis off,shading interp, colormap(hot) % 属性控制
% m=moviein(200);       % 创建一个15帧的动画矩阵,每一列是一帧(先开辟空间,运行快)
% for i=1:200  % 捕捉不同图像视角到动画矩阵中
%   view(-37.5+ 360/200 * (i-1),30)  % 旋转视图
%   m(:,i)=getframe;  % 当前图像添加到视频的一帧(列) %可以直接用 m(i)代替(慢)
% end
% movie(m);    % 播放动画,默认一次(多次、正逆序、固定速率,需要添加参数)

%% 颜色使用
% n=16;
% [X,Y,Z]=peaks(50);
% figure(1),surf(X,Y,Z);
% colormap(hot(n))            % colormap hsv(16)    选择颜色表,已经颜色种类数
% 
% figure(2),pcolor([1:n+1;1:n+1]')  % 显示某种颜色表的n种颜色(和surf显示颜色不同)
% hot(n);                       % 颜色序列 n*3
% 
% figure(3),rgbplot(hot)
% colorbar('horiz')        % colorbar('vert')等同于 colorbar
% 
% figure(4),
% pinkmap=sqrt(2/3*gray+1/3*hot);colormap(pinkmap); % 建立自己的颜色表,并安装
% surf(X,Y,Z);colorbar('vert');
% 
% figure(5)
%  pcolor([1:17;1:17]'),colormap(hsv(8));
%  title('Default Color Range')
%  caxis('auto');
%  colorbar
%  caxis;

%  x=-7.5: .5 : 7.5; y=x;  %  create a data set - the frame scmbrero
%  [X Y]=meshgrid(x,y);  %  create placid data
%  R=sqrt(X.^2+Y.^2)+eps;
%  Z=sin(R)./R
%  surf(X,Y,Z,Z)      %  缺省颜色  等效于  surf(X,Y,Z)
%  surf(X,Y,Z,-Z)     %  翻转缺省颜色
%  surf(X,Y,Z,X)      %  x轴
%  surf(X,Y,Z,X+Y)    %  XY对角线
%  surf(X,Y,Z,R)      %  中心辐射
%  surf(X,Y,Z,abs(del2(Z)))   %  拉普拉斯绝对值
%  [dZdx,dZdy]=gradient(Z);   % 曲面梯度
%  surf(X,Y,Z,abs(dZdx));     % x方向绝对梯度    
%  surf(X,Y,Z,abs(dZdy));     % y方向绝对梯度    
%  dz=sqrt(dZdx.^2+dZdy.^2);
%  surf(X,Y,Z,dz)             % 按斜率幅值变化

% [X,Y,Z]=peaks(32);  %  data to plot
% surfl(X,Y,Z) , colormap(copper) , title('Default Lighting') , shading interp
% figure,surfl(X,Y,Z,[7.5  30],[.55  .6  .4  10]) , shading interp
% figure,surfl(X,Y,Z,[-90  30],[.55  .6  2  10]) , shading interp
% brighten(0.8)    % 调整颜色表亮度, [0,1] 加量  [-1,0]变暗

%% 句柄图像
% Hf_fig =figure('Name','my figure','Color','blue')  %  自定义标题,背景,/'',''/...
% x=rand(2,4),y=rand(2,4)
% Hl_line=line(x,y)     % 4条直线,x,y同一列为一条直线, 对应行为其中一个端点
%                       % 点(x11,y11)和点(x12,y12)是其中一条直线的2个端点
% set(Hl_line(1))   % 显示第一条直线 可设置的属性 和 潜在值
% get(Hl_line(1))   % 显示第一条直线 属性 和 设置值

% x=-2*pi:pi/40:2*pi;  %  create data
% y=sin(x);  %  find the sine of x
% Hl_sin=plot(x,y)  %  plot sine and save line handle
% set(Hl_sin, 'Color' ,[1 .5 0], 'LineWidth' ,3)
% z=cos(x);  %  find the cosine of x
% hold on  %  keep the sine curve
% Hl_cos=plot(x,z);  %  plot the cosine and save the handle
% set(Hl_cos, 'Color' ,[.75  .75  1])  %  color it light blue
% hold off
% ------------------代替上面一段-----------------
% Hf_fig =figure('Name','my figure')
% x=-2*pi:pi/40:2*pi;y=sin(x); z=cos(x);
% Hl_lines=plot(x,y,'g',x,z);  
% set(Hl_lines(1),'Color',[1 .5 0], 'LineWidth' ,3)
% set(Hl_lines(2),'Color',[.75 .75 1])
% 
% % 句柄对象属性
% title( 'Handle Graphics Example', 'Color','blue' )  %  加标题, 设置 颜色
% Ht_text=get(gca, 'Title' )          %  获取属性'Title',即标题的句柄(属于坐标轴?)
% set(Ht_text, 'FontSize' ,16)        %  自定义字体大小
% xlabel('x');Ht_x=get(gca, 'Xlabel' ) 
% set(Ht_x, 'FontSize' ,16) 
% get(gcf);               % 窗口 可设置的属性 和 潜在值
% get(Ht_text)            % 标题的属性  
% get(Ht_text,'Parent')   %   (父对象是 坐标轴)
%  % 标题字符串和坐标轴的标志不包含在坐标轴的 ' Children ' 属性值里,而是保存在
%  %' Title ' 、 ' Xlabel ' 、 ' Ylabel ' 和 ' Zlabel ' 的属性内。创建
%  % 坐标轴对象时,这些文本对象就建立。title命令设置当前坐标轴内标题文本对象的 
%  % 'String' 属性。最终,标准MATLAB的函数title,xlabel,ylabel和zlabel不返回
%  % 句柄(不存在句柄),而只接受属性和数值参量。
%  % 但是都可以通过 h=get(gca,'Xlable')等获取,(实际也是坐标轴的一种属性)
%  
% Ha_asix=gca                         % 坐标轴的句柄
% ginput(1);
% disp('current selected handle: '),
% h=gco()                              % 鼠标点击图像窗口中的一个对象                     
% get(h,'Children')                    % 获取父对象的句柄
% set(h, 'Color' ,rand(1,3));          % 设置选中对象的颜色
% 
% get(gcf,'Children')  % 窗口的子对象     这里是 Ha_asix
% get(gca,'Children')  % 坐标轴的子对象   这里是 Hl_lines

% 查找绿色的线条
% Hf_fig2=figure('Name','my figure2')
% x=-pi:pi/20:pi;                     %  create some data
% y=sin(x);z=cos(x);
% Hl_lines2=plot(x,y, 'r' ,x,z, 'g' );            %  plot two lines in red and green
%Ht_l=legend('sin(x)','cos(x)');
% Hl_lines=get(gca, 'Children' );               %  坐标轴子对象,即曲线的句柄
% for k=1:size(Hl_lines)                        %  找绿线
%     if get(Hl_lines(k), 'Color' )==[0 1 0]
%         Hl_green=Hl_lines(k)
%     end
% end

% Hf_all=get(0, 'Children' );         %  桌面子对象(窗口)的句柄
% Ha_all=[],Hx_all=[],Hl_all=[],Hl_green=[];
% for k=1:length(Hf_all)
%     Ha_all=[Ha_all;get(Hf_all(k), 'Children' )];  %  窗口子对象(坐标轴)的句柄
% end
% for k=1:length(Ha_all)
%     Hx_all=[Hx_all;get(Ha_all(k), 'Children' )];  %  坐标轴子对象(曲线...)的句柄
% end
% for k=1:length(Hx_all)
%     if get(Hx_all(k), 'Type' )== 'line'
%         Hl_all=[Hl_all;Hx_all(k)];             %  筛选曲线句柄
%     end
% end
% for  k=1:length(Hl_all)
%     if get(Hl_all(k), 'Color' )==[0 1 0]
%         Hl_green=[Hl_green;Hl_all(k)];         %  曲线中查找绿线
%     end
% end

% H_all = findobj   % 找到所有句柄
% Hl_green=...
%     findobj(0, 'Type' ,'line','Color',[0  1  0])  %'0’,找到桌面所有子对象中的绿色线条
% Hl_green=...
%     findobj(2, 'Type' ,'line','Color','g')        % '1', 找到窗口1中子对象的绿色线条
% Hl_green= findobj('Type' ,'line','Color','g')     %  不设定范围(类似‘0’)

% 鼠标选择对象
% h=figure(1),h11=plot(0:1,0:1),hold on,h12=plot(0:1,0.8:-0.5:0)
% h2 = get(h,'Children')
% ginput,hx=gco
% set(hx,'Color','y')

% 位置 默认xoy,左下角原点
% 
% get(gcf,'Position');

%缺省设置   (版本不同。。。)
% set(0,'DefaultFigureColor','r')    %  set the default at the root level
% %set(gcf,'DefaultFigureColor','g')  %  current figure level default
% %set(gca,'DefaultFigureColor','b')  %  current axes level default
% Hl_rand=plot(rand(10,2))    %  plot a yellow line
% set(H1_rand,'Color','default')    %  the line become blue 
% set(gca,'DefaultFigureColor','remove')  %  the axes level default is removed
% set(Hl_rand,'Color','default')     %  the line become green
% close(gcf)  %  close the window
% Hl_rand=plot(rand(size([1:10])))   %  plot a yellow line in a new window
% set(Hl_rand,'Color','default')    %  the line becomes red

% figure('NumberTitle','off','Name','My figure')  % 改'Figure No:n Name' 为 'Name'
% set(1,'Pointer','crosshair')                    %  窗口中 鼠标为十字形

% xdata = [2     2     0     2     5;
%          2     8     2     4     5;
%          8     8     2     4     8];
% ydata = [4     4     4     2     0;
%          8     4     6     2     2;
%          4     0     4     0     0];
% cdata = [15     0     4     6    10;
%          1     2     5     7     9;
%          2     3     0     8     3];
% p = patch(xdata,ydata,cdata,'Marker','o','MarkerFaceColor','flat','FaceColor','none') 
% set(p,'EdgeColor','g')

% load clown
% surface(peaks,flipud(X),...                 % 在surf上覆盖clown图像
%    'FaceColor','texturemap',...
%    'EdgeColor','none',...
%    'CDataMapping','direct')
% colormap(map)
% view(3)         % 3D视角

% load mandrill
% figure('color','k')    
% image(X)
% colormap(map) 
% axis off          % 去掉坐标轴和刻度
% axis image        % 保持图像长宽比例
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

aworkholic

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值