strX = num2str(X,format) converts the matrix, X into a string with a format specified by the sprintf command. The table below shows how can be displayed in different formats with the sprintf command. Command Result Command Result sprintf('% .0e \n',pi) 3e+00 sprintf('% .3e \n',pi) 3.142e+00 sprintf('% .0f \n',pi) 3 sprintf('% .4f \n',pi) 3.1416 sprintf('% .0g \n',pi) 3 sprintf('% .3g \n',pi) 3.14 \n line termination character. e exponential notation (number to right of decimal point gives number of digits displayed after the decimal point in the number). f fixed-point notation (number to right of decimal point gives number of digits displayed after the decimal point in the number). g general notation: fixed-point or exponential notation, whichever is shorter (number to right of decimal point gives the total number of digits displayed). e.g. X = pi strX = num2str(X,'% .5f \n') = 3.14159 Exercise 4 Open your m-script ex3.m and immediately save it as ex4.m. The m-script can be extended to annotate the graph of the sinusoidal function as shown in figure 1 with: Title with equation and numerical values for A and T. Axes labelled. Lines and text added to show amplitude and period. Numerical values displayed for angular velocity and frequency f. Fig. 2. Annotated plot of sinusoidal function of equation (3.1). The complete m-script ex4.m for figure 1 is at the end of this Chapter. Work through it line by line making sure you understand each part of the code. The handles to figure window elements are given by the prefix h_ in the code. Change the program and observe the figure window to test your understanding code. Data displayed on the graph and the plot window The way in which data is displayed on the graph is through the line properties. You can change the appearance of the plotted data using the following Matlab commands. Property Properties values (default)
vertical and is numbered from top to bottom. The J- axis is horizontal and is numbered from left to right. axis XY Puts Matlab into its default "Cartesian" axes mode. The coordinate system origin is at the lower left corner. The X-axis is horizontal and is numbered from left to right. The Y-axis is vertical and is numbered from bottom to top. axis equal Sets the aspect ratio so that equal tick mark increments on the X and Y axes are equal in size. This makes a circle look like a circle instead of an ellipse and a square, a square, not a rectangle. axis image The same as axis equal except that the plot box fits tightly around the data. axis square Makes the current axis box square in size. axis normal Restores the current axis box to full size and removes any restrictions on the scaling of the units. This undoes the effects of axis square and axis equal. axis on axis off turns axis labeling, tick marks,grid and background back on. turns off all axis labeling, tick marks, grid and background. axis(H,...) Changes the axes handles listed in vector H. ??? axes axes('position', rect) Create axes in arbitrary positions. Opens up an axis at the specified location and returns a handle to it. Axes, by itself, creates the default full-window axis and returns a handle to it. rect = [left, bottom, width, height] specifies the location and size of the side of the axis box, relative to the corner of the Figure window, in normalized units where (0,0) is the lower-left corner and (1.0,1.0) is the upper-right. axes(H) makes the axis with handle H current. Execute get(H) to see a list of axes object properties and their current values Exercise 5 Lissajous figures are created when two sinusoidal functions are plotted against each other. Let the two sinusoidal functions (called wave functions) be given by equations (2) and (3) , (2) , (3) where = 2 f = 2/T. Changing the values assigned to both the frequencies f1 and f2 and the phases 1 and 2 produces interesting changes to the Lissajous figure. Create two figure windows for the plots Figure X Data Y Data 1: Sinusoidal Functions t