Some Tips about Using Matlab on unixapps1 (CATS)

 

 

First, logon to the UNIX machine called unixapps1 from any of the Terminals at Russ center (or Library Annex).

 

Important : Every time you begin, you must first type in :

 

     setenv DISPLAY networknumber:0

 

to be able to see plots displayed on screen.

 

Note : You must append :0 at the end of the networknumber.

 

The networknumber can be found under the workstation terminal thatyou are using. For example, if it shows 130.108.11.75, type in :

 

     setenv DISPLAY 130.108.11.75:0

 

MATLAB is a Software Environment. To activate MATLAB, type in :

 

     matlab

 

Once the MATLAB program is activated, it will ask you to select a printer according to where you are working (russ1 or Library or Allyn). The prompt >> would tell you that you are inside MATLAB, as follows :

 

>>

 

MATLAB is an interactive environment. If you type in a command, it will respond with the result on the screen. For example if you type :

 

>>a=10

 

MATLAB will respond with :

 

a =

     10

 

If you wish to suppress displaying the answer on screen, end with a semicolon, e.g.,

 

>> a=10;

 

This time matlab will just show you a new prompt :

 

>>

 

In either case, the variable a is assigned a value of 10. Hence, if you type

 

>> a + 2

 

Matlab will respond with :

 

ans =

       12

 

To leave the matlab environment, type in :

 

>> exit

 

 

Example-1 :Plot a straight line having a slope of 0.7 and y-axis cutoff = 20

 

 

>>x=0:100:2;             % Creates the x-vector [0 2 4 ... 100]

>>y=0.7*x + 20;         % Creates the y-values [20 21.4 22.8 ... 90]

 

Let's see how it looks,

 

>>plot(x,y,'r')             % Try help plot to see how plot works

 

Now, try the following commands one by one and observe how the plot changes ....

 

>> axis([-10, 110, 0, 100])

 

>>title('My First Plot')      % Adds a title at the top (optional)

 

>>xlabel('x')                    % Labels the x-axis (optional)

 

>>ylabel('y=0.7x+20')     % Labels the y-axis (optional)

 

 

Example-2 : Plot the Sum of a Sine and a Cosine Waveforms

 

Now we will demonstrate how to plot a graph of sinusoidal functions :

 

f(t) = 2sin(2*pi*t) - cos(4*pi*t); t from -1 to 1 sec,with a sampling interval of 0.01 second (i.e., Sampling Frequency = 100Hz).

 

The following sequence of MATLAB commands will create the discrete sequences t and f. Next the function is plotted on the screen with a title and axis-labels. Then the plot is sent for a hardcopy output.

 

>>t=-1:0.01:1;                            % Creates the time-vector t of length 201

                                                    %-1:0.01:1 denotes begin:increment:end

 

>>f=2*sin(2*pi*t)-cos(4*pi*t);   % Creates the f-vector of length 201

 

 

>>plot(t, f)                            % Plots f(t) vs. t on a new window

 

>>title('My Second Plot')    % Adds a title at the top (optional)

 

>>xlabel('Time (s)')           % Labels the x-axis (optional)

 

>>ylabel('function f')          % Labels the y-axis (optional)

 

>>print -Pruss1                 % Sends a hardcopy of the plot to the

                                           % printer located at 152 RC

 

 

The plot-command used above adds lines from one point to another. If you wish to plot the actual points also, you could use :

 

>> plot(t,f,'*')                    % puts stars at actual values

 

Many other plotting options are also available on MATLAB. To find out what all are available, type in :

 

>> help plot                    % Displays information on plotting

 

 

On-Line Help and Demo facilities within Matlab

 

MATLAB offers an extensive on-line HELP-facility that describes all the available MATLAB-commands in great details. To obtain a list of commands for which HELP is available, type in :

 

>>help

 

To obtain help on a particular matlab-command of your interest, type in :

 

>>help matlab-command

 

MATLAB also has a number of interesting demos that you can view. To invoke the demo menu, type in

 

>> demo                   % Then select any demo from the menu

 

 

Creating Matlab using a text Editor

 

MATLAB commands are commonly used interactively for experimentation.For longer and more involved programs, it is convenient to create a file containing MATLAB-commands (known as m-files) using a text editor. Hence, you need to learn to use a text editor such as pico, textedit, vi, or emacs. If you are not familiar with any of these editiors, we suggest you use pico or textedit both of which are easy to learn and self-explanatory. To invoke an Editor when you are inside MATLAB, type :

 

>>!pico filename.m               % Invokes the pico editor

 

  or

 

>>!textedit filename.m          % Invokes the textedit editor

 

The filename may be anything that you want. For example, you could create a file called firstplot.m  by typing

 

>>!pico firstplot.m

 

and put in all the commands in the Example given above.

 

Once you have created the m-file, you can execute all the commands in the m-file by typing, for example :

 

>> firstplot                  % Sequentially executes all the commands

                                   % in the m-file called firstplot.m

 

NOTE : If you want to edit a file from UNIX (after exiting from MATLAB), you don't need the !-mark, just type :

 

       pico firstplot.m  

 

Note : Any Unix command can be invoked from the MATLAB environment by adding an ! at the beginning of the unix-command.

 

To get a hardcopy of your program listing, use the unix-command :

 

      russ1 firstplot.m     % If you are at 152RC

 

            or

 

     lps40 firstplot.m     % If you are at the Library Annex