MATLAB WORKSHOP MATLAB WORKSHOP 3 Select the Cut, Copy, or Paste option from the Edit menu or use standard

  • View

  • Download

Embed Size (px)

Text of MATLAB WORKSHOP MATLAB WORKSHOP 3 Select the Cut, Copy, or Paste option from the Edit menu or use...


    MATLAB Graphics

    Mr. Amit Kumar


    Introduction: MATLAB provides a wide variety of techniques to display data graphically. Interactive tools enable you to manipulate graphs to achieve results that reveal the most information about your data. You can also annotate and print graphs for presentations, or export graphs to standard graphics formats for presentation in web browsers or other media. Plotting Your Data: The process of constructing a basic graph to meet your presentation graphics requirements is outlined in the following table. The table shows seven typical steps and some example code for each. If you are performing analysis only, you may want to view various graphs just to explore your data. In this case, steps 1 and 3 may be all you need. If you are creating presentation graphics, you may want to fine-tune your graph by positioning it on the page, setting line styles and colors, adding annotations, and making other such improvements. Step Typical Code

    1 Prepare your data x = 0:0 . 2:12; y1 = bessel(1,x) ; y2 = bessel(2,x); y3 = bessel(3,x); 2 Select a window and position a figure(1) plot region within the window subplot(2,2,1) 3 Call elementary plotting h = plot(x,y1,x,y2,x,y3); function 4 Select line and marker s e t ( h , 'LineWidth',2,{'LineStyle' } , { ' - - ' ; ' : ' ; ' - . ' } ) characteristics s e t ( h , { ' C o l o r ' } , {'r';'g';'b'}) 5 Set axis limits, tick marks, and axis([0 12 -0.5 1 ] ) grid lines grid on 6 Annotate the graph with axis xlabel('Time') labels, legend, and text ylabel('Amplitude') legend(h,'First','Second','Third') title('Bessel Functions') [ y , i x ] = min(y1); text( x ( i x ) ,y,'Firs t Min \r ightarrow', ... 1 HorizontalAlignment','right') 7 Export graph print -depsc -tiff -r200 myplot

    Back to contents

  • MATLAB WORKSHOP Anatomy of a Graph MATLAB® plotting functions and tools direct their output to a window that is separate from the Command Window. In MATLAB this window is referred to as a figure. For example, the following picture illustrates a graph of the Bessel function, highlighting the basic components of the graph. By default, MATLAB uses line style and color to distinguish the data sets plotted in the graph. However, you can change the appearance of these graphic components or add annotations to the graph to help explain your data for presentation. Editing a Plot MATLAB formats a graph to provide readability, setting the scale of axes, including tick marks on the axes, and using color and line style to distinguish the plots in the graph. However, if you are creating presentation graphics, you may want to change this default formatting or add descriptive labels, titles, legends and other annotations to help explain your data.

    MATLAB supports two ways to edit the plots you create.

    • Using the mouse to select and edit objects interactively • Using MATLAB functions at the command-line or in an M-file

    Interactive Plot Editing If you enable plot editing mode in the MATLAB figure window, you can perform point-and- click editing of your graph. In this mode, you can change the format of objects in your graph by double-clicking on the object and changing the values of its properties. In plot editing mode, you access the properties through the graphical user interface, called the Property Editor. For more information about interactive editing, see "Using Plot Editing Mode". For information about editing object properties in plot editing mode, see "Using the Property Editor".

    Back to contents

  • MATLAB WORKSHOP Using Functions to Edit Graphs If you prefer to work from the MATLAB command line or if you are creating an M-file, you can use MATLAB commands to edit the graphs you create. Taking advantage of MATLAB's Handle Graphics system, you can use the set and get commands to change the properties of the objects in a graph. Using Plot Editing Mode The MATLAB figure window supports a point-and-click style editing mode that you can use to customize the appearance of your graph. This section describes how to start plot edit mode and perform basic editing tasks, including: • "Selecting Objects in a Graph" • "Cutting, Copying, and Pasting Objects" • "Moving and Resizing Objects" • "Editing Objects in a Graph" • "Saving Your Work" • "Changing Your View of an Axes"

    Starting Plot Edit Mode: Before you can select objects in a figure by clicking on them, you must activate plot editing mode. There are several ways to activate plot edit mode: • Choose the Edit Plot option on the figure window Tools menu.

    Back to contents


  • MATLAB WORKSHOP • Click on the selection button in the figure window toolbar. • Choose an option from the Edit or Insert menu. For example, if you choose

    the Axes Properties option on the Edit menu, MATLAB activates plot edit mode and the axes appear selected.

    • Run the plotedit command in the MATLAB command window. When a figure window is in plot edit mode, the Edit Plot option on the Tools menu is checked and the selection button in the toolbar is highlighted. Exiting Plot Edit Mode To exit plot edit mode, click the selection button or click the Edit Plot option on the Tools menu. When plot edit mode is turned off, the selection button is no longer highlighted. Selecting Objects in a Graph To select an object in a graph: 1 Start plot edit mode. 2 Move the cursor over the object and click on it.

    Selection handles appear on the selected object. Selecting Multiple Objects To select multiple objects at the same time: 1 Start plot edit mode. 2 Move the cursor over an object and shift-click to select it. Repeat for each object you want to select. You can perform actions on all of the selected objects. For example, to remove a text annotation and an arrow annotation from a graph, select the objects and then select Cut from the Edit menu. Deselecting Objects To deselect an object, move the cursor off the object onto the figure window background and click the left mouse button. You can also shift-click on a selected object to deselect it. Cutting, Copying, and Pasting Objects To cut an object from a graph, or copy and paste an object in a graph, perform these steps.

    1 Start plot edit mode.

    2 Select the object. Back to contents



    3 Select the Cut, Copy, or Paste option from the Edit menu or use standard shortcut keys for your platform.

    Alternatively, with plot edit mode enabled, you can right-click on an object and then select an editing command from the context menu associated with the object. Note If you cut an axes label or title and then paste it back into a figure, the label or title is no longer anchored to the axis. If you move the axes, the label or title will not move with the axes. Moving and Resizing Objects To move or resize an object in a graph, perform these steps.

    1 Start plot edit mode.

    2 For axes objects only: unlock the axes by right-clicking on it and

    choosing Unlock Axes Position from the context menu.

    3 Select the object. Selection handles appear on the object. To move the object, drag it to the new location. To resize the object, drag a selection

    handle. Note You can move, but cannot resize text objects. Editing Objects in a graph In MATLAB, every object in a graph supports a set of properties that control the graph's appearance and behavior. For example, line objects support properties that control thickness, color, and line style. In plot edit mode, MATLAB provides a graphical user interface to object properties called the Property Editor. The Property Editor is a dialog box that supports a specific set of tabbed panels for each object you can select. For more information, see "Using the Property Editor". Saving Your Work After editing a graph, you can:

    • Save your work in a format that can be opened during another MATLAB session. • Save your work in a format that can be used by other applications.

    Back to contents



    Saving a Graph in MAT-File Format MATLAB supports a binary format in which you can save figures so that they can be opened in subsequent MATLAB sessions. MATLAB assigns these files the . f i g file name extension.

    To save a graph in a figure file:

    1 Select Save from the figure window File menu or click the Save button on the toolbar. If this is the first time you are saving the file, the Save As dialog box appears.

    2 Make sure that the Save as type is Fig-file.

    3 Specify the name you want assigned to the figure file.

    4 Click OK.

    The graph is saved as a figure file (.fig), which is a binary file format used to store figures. You can also use the saveas command.If you want to save the figure in a format that can be used by another application, see "Saving to a Different Format (Exporting Figures". Opening a Figure File To open a figure file, perfor