3 March 2013

Introduction to MATLAB graphics


    MATLAB has a large number of functions associated with graphical output. If you'd like to explore the possibilities use help plot or help plot 3 for 3-dimensional plots, or run the MATLAB demo (by typing demo) and look at the information on visualization and graphics. We start with basic plotting routines and look at some fancy graphics to get a taste of MATLAB’s abilities.

     Suppose that the maximum and minimum temperature (in degrees Celsius) recorded from 12 to 18 January are 23, 27, 21, 28, 24, 25, 26, and 11, 10, 15, 15, 14, 15, 12 respectively.

      1) Set up the vector 'date' to have elements from 12 to 18, and the vectors 'maxtemp' and 'mintemp' to contain the temperature data given above. It does not matter whether the vectors are row or column vectors.

       All the vectors are of the same length so it is possible to plot one against the other.

     2) Create a plot of the maximum temperatures by typing plot(date, maxtemp) The x-axis variable is listed first. This graph will be created in a window called Figure No. 1. If this window is not visible select Figure No. 1 under Window on the menu. Your graph should look like the one below.


     3) Now type plot(date,mintemp) . The previous plot disappears and the new plot is displayed.

We can also plot both graphs in one figure:

        4) Type plot(date,maxtemp); followed by hold on; Then type plot(date, mintemp); followed by hold off; hold on tells MATLAB to keep the old plot and add the new graph to it. hold off turns the hold-feature off again. We could also have used plot(date, maxtemp, date, mintemp); which tells MATLAB to plot maxtemp against date and then mintemp against date in the same graph. The colors of the graphs are then different.


       5) Select Insert from the figure window and then Title. A text box will appear in the figure window. Type '12-18 January'. Now add the label 'date' to the x-axis and 'temp (Celsius)' to the y-axis using the appropriate selections under Insert.


       6) Select Edit and then Axes properties. Un-select Auto for X (click in the Auto box to remove the tick). Then change the limits of 12 and 18 for the x-axis to 11.5 and 18.5 respectively. Now change the limits for the y-axis to 9 and 30. If the Immediately apply box at the bottom of the Axes Properties window is not ticked you must click OK to see the changes.


    7) Click on the graph corresponding to the maximum temperature. Then select Current Object Properties under Edit in the figure window. Select No line (none) under Line Style. Under Marker Properties, select a marker of your choice, eg. Six-pointed star. Choose a nice color.

     8) Repeat for the graph corresponding to the minimum temperature with a different color and marker.

     9) To add a legend, select Insert then Legend. You can change the description of the graphs in the legend by double clicking on the text in the figure window. Change the descriptions to 'maxima' for the maximum temperature graph and 'minima' for the minimum temperature graph.


    10) Select Insert, then Text (or click on the A button on the menu). Move your mouse to the plot window. Position the cursor near the highest point in the graph and click. Type 'highest' in the field.

    The final graph is shown below:



No comments:

Post a Comment

Factory Method Design Pattern in Java

The Factory Design Pattern is probably the most used design pattern in modern programming languages like Java.  Here we discuss about it an...