Matlab绘制图像及图像的处理

一 绘制函数图像

matlab平面绘制函数图像有多个函数,plot,ezplot等。

1.1 plot函数

查看matlab的帮助文件可知plot函数的调用格式有


  PLOT Linear plot.

    PLOT(X,Y) plots vector Y versus vector X. If X or Y is a matrix,
    then the vector is plotted versus the rows or columns of the matrix,
    whichever line up. If X is a scalar and Y is a vector, disconnected
    line objects are created and plotted as discrete points vertically at
    X.

    PLOT(Y) plots the columns of Y versus their index.
    If Y is complex, PLOT(Y) is equivalent to PLOT(real(Y),imag(Y)).
    In all other uses of PLOT, the imaginary part is ignored.

    Various line types, plot symbols and colors may be obtained with
    PLOT(X,Y,S) where S is a character string made from one element
    from any or all the following 3 columns:

  b blue                       . point                           - solid
  g green                      o circle                          : dotted
  r red                        x x-mark                          -. dashdot 
  c cyan                       + plus -- dashed 
  m magenta                    * star (none) no line
  y yellow                     s square
  k black                      d diamond
  w white                      v triangle (down)
                               ^ triangle (up)
                               < triangle (left)
                               > triangle (right)
                               p pentagram
                               h hexagram

    For example, PLOT(X,Y,'c+:') plots a cyan dotted line with a plus
    at each data point; PLOT(X,Y,'bd') plots blue diamond at each data
    point but does not draw any line.

    PLOT(X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...) combines the plots defined by
    the (X,Y,S) triples, where the X's and Y's are vectors or matrices
    and the S's are strings.

    For example, PLOT(X,Y,'y-',X,Y,'go') plots the data twice, with a
    solid yellow line interpolating green circles at the data points.

 

1.2 ezplot函数 

查看matlab的帮助文件可知plot函数的调用格式有

EZPLOT   Easy to use function plotter
EZPLOT(FUN) plots the function FUN(X) over the
default domain -2*PI < X < 2*PI, where FUN(X) is an explicitly defined function of X. EZPLOT(FUN2) plots the implicitly defined function FUN2(X,Y) = 0 over the default domain -2*PI < X < 2*PI and -2*PI < Y < 2*PI. EZPLOT(FUN,[A,B]) plots FUN(X) over A < X < B. EZPLOT(FUN2,[A,B]) plots FUN2(X,Y) = 0 over A < X < B and A < Y < B. EZPLOT(FUN2,[XMIN,XMAX,YMIN,YMAX]) plots FUN2(X,Y) = 0 over XMIN < X < XMAX and YMIN < Y < YMAX. EZPLOT(FUNX,FUNY) plots the parametrically defined planar curve FUNX(T) and FUNY(T) over the default domain 0 < T < 2*PI. EZPLOT(FUNX,FUNY,[TMIN,TMAX]) plots FUNX(T) and FUNY(T) over TMIN < T < TMAX. EZPLOT(FUN,[A,B],FIG), EZPLOT(FUN2,[XMIN,XMAX,YMIN,YMAX],FIG), or EZPLOT(FUNX,FUNY,[TMIN,TMAX],FIG) plots the function over the specified domain in the figure window FIG. EZPLOT(AX,...) plots into AX instead of GCA or FIG. H = EZPLOT(...) returns handles to the plotted objects in H. Examples: The easiest way to express a function is via a string: ezplot('x^2 - 2*x + 1') One programming technique is to vectorize the string expression using the array operators .* (TIMES), ./ (RDIVIDE), .\ (LDIVIDE), .^ (POWER). This makes the algorithm more efficient since it can perform multiple function evaluations at once. ezplot('x.*y + x.^2 - y.^2 - 1') You may also use a function handle to an existing function. Function handles are more powerful and efficient than string expressions. ezplot(@humps) ezplot(@cos,@sin) EZPLOT plots the variables in string expressions alphabetically. subplot(1,2,1), ezplot('1./z - log(z) + log(-1+z) + t - 1') To avoid this ambiguity, specify the order with an anonymous function: subplot(1,2,2), ezplot(@(z,t)1./z - log(z) + log(-1+z) + t - 1) If your function has additional parameters, for example k in myfun: %-----------------------% function z = myfun(x,y,k) z = x.^k - y.^k - 1; %-----------------------% then you may use an anonymous function to specify that parameter: ezplot(@(x,y)myfun(x,y,2))

注意plot函数的输入是数值的向量或者标量(也可以是多维的矩阵),而ezplot的第一个输入是函数句柄。

二 matlab绘制图像后的标注处理

2.1 gcf参量

gcf  可以获取当前窗口句柄

2.2 为x轴及y轴设置标注,为图像绘制网格线

 

xlabel()及ylabel()为x轴及y轴设置标注

另外利用 grid或者grid on来为图像绘制网格线。

2.3 legengd为各个图线做说明

 

legend()函数,查看matlab的使用帮助可知legend函数的调用格式有

 LEGEND Display legend.
    LEGEND(string1,string2,string3, ...) puts a legend on the current plot
    using the specified strings as labels. LEGEND works on line graphs,
    bar graphs, pie graphs, ribbon plots, etc.  You can label any
    solid-colored patch or surface object.  The fontsize and fontname for
    the legend strings matches the axes fontsize and fontname.
 
    LEGEND(H,string1,string2,string3, ...) puts a legend on the plot
    containing the handles in the vector H using the specified strings as
    labels for the corresponding handles.
 
    LEGEND(M), where M is a string matrix or cell array of strings, and
    LEGEND(H,M) where H is a vector of handles to lines and patches also
    works.
 
    LEGEND(AX,...) puts a legend on the axes with handle AX.
 
    LEGEND OFF removes the legend from the current axes and deletes
    the legend handle.
    LEGEND(AX,'off') removes the legend from the axis AX.
 
    LEGEND TOGGLE toggles legend on or off.  If no legend exists for the
    current axes one is created using default strings. The default
    string for an object is the value of the DisplayName property
    if it is non-empty and otherwise it is a string of the form
    'data1','data2', etc.
    LEGEND(AX,'toggle') toggles legend for axes AX
 
    LEGEND HIDE makes legend invisible.
    LEGEND(AX,'hide') makes legend on axes AX invisible.
    LEGEND SHOW makes legend visible. If no legend exists for the
    current axes one is created using default strings.
    LEGEND(AX,'show') makes legend on axes AX visible.
 
    LEGEND BOXOFF  makes legend background box invisible when legend is
    visible.
    LEGEND(AX,'boxoff') for axes AX makes legend background box invisible when
    legend is visible.
    LEGEND BOXON makes legend background box visible when legend is visible.
    LEGEND(AX,'boxon') for axes AX making legend background box visible when
    legend is visible.
 
    LEGH = LEGEND returns the handle to legend on the current axes or
    empty if none exists. 
 
    LEGEND(...,'Location',LOC) adds a legend in the specified
    location, LOC, with respect to the axes.  LOC may be either a
    1x4 position vector or one of the following strings:
        'North'              inside plot box near top
        'South'              inside bottom
        'East'               inside right
        'West'               inside left
        'NorthEast'          inside top right (default for 2-D plots)
        'NorthWest'           inside top left
        'SouthEast'          inside bottom right
        'SouthWest'          inside bottom left
        'NorthOutside'       outside plot box near top
        'SouthOutside'       outside bottom
        'EastOutside'        outside right
        'WestOutside'        outside left
        'NorthEastOutside'   outside top right (default for 3-D plots)
        'NorthWestOutside'   outside top left
        'SouthEastOutside'   outside bottom right
        'SouthWestOutside'   outside bottom left
        'Best'               least conflict with data in plot
        'BestOutside'        least unused space outside plot
    If the legend does not fit in the 1x4 position vector the position
    vector is resized around the midpoint to fit the preferred legend size.
    Moving the legend manually by dragging with the mouse or setting
    the Position property will set the legend Location property to 'none'.
 
    LEGEND(...,'Orientation',ORIENTATION) creates a legend with the
    legend items arranged in the specified ORIENTATION. Allowed
    values for ORIENTATION are 'vertical' (the default) and 'horizontal'.
 
    [LEGH,OBJH,OUTH,OUTM] = LEGEND(...) returns a handle LEGH to the
    legend axes; a vector OBJH containing handles for the text, lines,
    and patches in the legend; a vector OUTH of handles to the
    lines and patches in the plot; and a cell array OUTM containing
    the text in the legend.
 
    Examples:
        x = 0:.2:12;
        plot(x,bessel(1,x),x,bessel(2,x),x,bessel(3,x));
        legend('First','Second','Third');
        legend('First','Second','Third','Location','NorthEastOutside')
 
        b = bar(rand(10,5),'stacked'); colormap(summer); hold on
        x = plot(1:10,5*rand(10,1),'marker','square','markersize',12,...
                 'markeredgecolor','y','markerfacecolor',[.6 0 .6],...
                 'linestyle','-','color','r','linewidth',2); hold off
        legend([b,x],'Carrots','Peas','Peppers','Green Beans',...
                  'Cucumbers','Eggplant')

 

2.4 text函数在图像某点做标注


查看text函数在matlab中的使用帮助

 TEXT   Text annotation.
    TEXT(X,Y,'string') adds the text in the quotes to location (X,Y)
    on the current axes, where (X,Y) is in units from the current
    plot. If X and Y are vectors, TEXT writes the text at all locations
    given. If 'string' is an array the same number of rows as the
    length of X and Y, TEXT marks each point with the corresponding row
    of the 'string' array.
 
    TEXT(X,Y,Z,'string') adds text in 3-D coordinates.
 
    TEXT returns a column vector of handles to TEXT objects, one
    handle per text object. TEXT objects are children of AXES objects.
 
    The X,Y pair (X,Y,Z triple for 3-D) can be followed by 
    parameter/value pairs to specify additional properties of the text.
    The X,Y pair (X,Y,Z triple for 3-D) can be omitted entirely, and
    all properties specified using parameter/value pairs.
 
    Execute GET(H), where H is a text handle, to see a list of text
    object properties and their current values. Execute SET(H) to see a
    list of text object properties and legal property values.

 

例如

plot(0:pi/20:2*pi,sin(0:pi/20:2*pi))
text(pi,0,' \leftarrow sin(\pi)','FontSize',18)

 

得到的图像为

2.5 设置使绘制的图像窗口不显示

 

set(h,'visible','off');其中h可以是图像的函数句柄,也可以是数字1,2(当前图像的名字是figure(1)就是1,反之figure(2)就是2)
要设置图像显示  set(h,'visible','on')
h=figure(1);
x=0:0.01:1;
y=sin(x);
plot(x,y);
set(h,'visible','off'); 

 2.6 设置使绘制的图像工具栏不显示

figure('menubar','none');可去掉菜单栏

可以在打开你的m文件之后,在Matlab的命令行输入以下指令来恢复显示Scope的Figure菜单栏: 

>> set(0,'ShowHiddenHandles','on');  

>> set(gcf,'menubar','figure')

2.7 绘制的图像自动保存

2.7.1 saveas命令

查看saveas命令的matlab帮助

 SAVEAS Save Figure or Simulink block diagram in desired output format
    SAVEAS(H,'FILENAME')
    Will save the Figure or Simulink block diagram with handle H to file 
    called FILENAME. 
    The format of the file is determined from the extension of FILENAME.
 
    SAVEAS(H,'FILENAME','FORMAT')
    Will save the Figure or Simulink block diagram  with handle H to file 
    called FILENAME in the format specified by FORMAT. FORMAT can be the 
    same values as extensions of FILENAME. 
    The FILENAME extension does not have to be the same as FORMAT.  
    The specified FORMAT overrides FILENAME extension.
 
    Valid options for FORMAT are:
 
    'fig'  - save figure to a single binary FIG-file.  Reload using OPEN. 
    'm'    - save figure to binary FIG-file, and produce callable
             M-file for reload.
    'mfig' - same as M.
    'mmat' - save figure to callable M-file as series of creation commands 
             with param-value pair arguments.  Large data is saved to MAT-file.
             Note: MMAT Does not support some newer graphics features. Use
                   this format only when code inspection is the primary goal.
                   FIG-files support all features, and load more quickly. 

saveas的三个参数:(1)图形句柄,如果图形窗口标题栏是“Figure 3”,则句柄就是3.可以用gcf参量抓取当前图像句柄。

                          (2)文件名。是字符串。

                          (3)单引号字符串,指定存储格式。

实际上可以只输入两个参量,利用文件名直接强制转换成我们想要的图片格式,例如

saveas(gcf,['D:\MATLAB7\work','mypicture','.jpg']);

或者直接

saveas(gcf,'D:\MATLAB7\work\mypicture.jpg');

2.7.2 print命令

print命令本来是matlab用来打开打印机的命令,也可以用来保存图像。

print的三个参数:(1)图形句柄,如果图形窗口标题栏是“Figure 3”,则句柄就是3.用gcf可以获取当前窗口句柄。

                        (2)单引号字符串,指定存储格式。

                               png格式:'-dpng'

                               jpeg:    '-djpeg',

                               tiff: '-dtiff'

                               bmp: '-dbitmap'

                         (3)文件名。

 例如

>> x=-pi:2*pi/300:pi; 
>> y=sin(x);
>> plot(x,y);
>> print(gcf,'-dpng','abc.png') % 保存为png格式的图片。

  >> figure(2)     %新建一个句柄为2的图形窗口。

  >> plot(x,cos(x));    %在句柄为2的图形窗口上画图。

  >> grid 

  >> print(2,'-djpeg','C:\abc.jpeg'); %将句柄为2的图形保存为jpeg/jpg格式的图片,文件名为'C:\abc.jpeg'。

 

 

 

posted @ 2014-10-28 10:47  雨过江南00  阅读(1787)  评论(0编辑  收藏  举报