User Tools

Site Tools


gibson:teaching:spring-2016:math445:lecture:scripts

Math 445 lecture 3: scripts and plotting

topics

  Matlab scripting
  linestyles and plot modifiers: 
    xlabel, ylabel, title, legend, axis, grid, clf, 
  load, save
  plot, semilogy, semilogx, loglog
  what, who, pwd, ls 

Matlab scripting

A Matlab script is simply a list of Matlab commands in a plain-text (ASCII) file with a .m filename extension. If you call or execute the script by its name at the Matlab prompt without the .m extension, Matlab will read the file and execute the commands sequentially. For example, here is a Matlab script that will make a plot of $\sin x$ and $\cos x$, complete with labeled axes and a legend.

| plotsincos.m
% plot cos(x) and sin(x) over 0 < x < 2pi
x = linspace(0, 2*pi, 100);
plot(x, cos(x), 'b-', x, sin(x), 'r-');
xlabel('x');
legend('cos x', 'sin x');
title('two trig functions')
grid on
axis equal
axis([0 2*pi -1 1])

If you download this file and place it in your current working directory (folder), Matlab will be able to find it and execute it. In my case, Matlab is running in the directory /home/gibson/math445.

>> pwd       % print working directory
 
ans =
 
/home/gibson/math445
 
>> what      % print names of Matlab scripts in working directory
 
MATLAB Code files in the current folder /home/gibson/math445

When the script is run, Matlab opens a new figure window and draws the plot

>> plotsincos % execute the plotsincos script

Line styles and markers

The above plotting script used the matlab linestyle codes 'b-' and 'r-' to generate blue and red curves for $\cos x$ and $\sin x$. Matlab's help function provides a pretty good description of how line and marker style codes work.

>> help plot
 
   % clip out irrelevant stuff here 
 
   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 modifiers

Some helpful commands for modifying plots

xlabel('foo') % add an x label
ylabel('bar') % add a y label
title('baz')  % add a title
legend('first', 'second', 'third') % add a legend with these labels
axis on       % turn on axes and ticks (default)
axis off      % turn off axes and ticks
axis square   % make the plot square
axis equal    % scale the x and y axes the same
axis([xmin xmax ymin ymax]) % force these specific bounds on the axes
grid on       % turn on grid markers
grid off      % turn off grid markers
hold on       % hold the current plot; the next plot will be plotted on top of this one
hold off      % release the current plot; the next plot will erase this one
clf           % clear figure (erase current plot)
figure(2)     % open new figure window, numbered 2
gibson/teaching/spring-2016/math445/lecture/scripts.txt · Last modified: 2016/02/03 13:54 by gibson