unix mat lab intro
TRANSCRIPT
-
7/30/2019 Unix Mat Lab Intro
1/4
Introduction to UNIX and MATLABJoseph C. Slater
Jason J. BlairJanuary 3, 1997
M
ATLAB
(
MAT
rix LAB
oratory) is a mathematical computing package that combines
numerical analysis, matrix computation, and advanced graphics in a user-friendly environment.M
ATLAB
is a language that is similar C or FORTRAN but has greatly enhanced capabilities.Programs are written and stored in script
orfunction files
in ASCII (or text) format calledM-files
that carry the .
m
extension (e.g. vtb1_1.m
).
M
ATLAB
is available via accounts on gamma, which can be accessed from the X-terminal labin RUSS 152. To login to use M
ATLAB
, you must first connect to gamma. Do this by clicking onthe gamma button and then clicking on the connect button. When prompted, enter your usernamethen password. The first time you log in you should change your password. Do this with the
yppasswd
command at the prompt. Note: UnIX Is caSe SenSItIve. You next need to set yourDISPLAY environment so the main server knows where you are. This will enable your graphics
display to be sent to the X-Windows terminal that you are using. Do this by typingsetenv
DISPLAY
machine_number:0.0
where terminal number is something like 130.108.11.13. Besure to include all numbers and to put a:0.0
on the end (this should be displayed on the front of theterminal). Also, remember the case sensitivity. (If you are running at home without X-terminalcapabilities, you can use the command unsetenv DISPLAY
and type terminal
at the M
ATLAB
prompt. This does not work well, but you may be able to at least display the graphics.) To useemail, I recommend trying pine
.
To start M
ATLAB
, type matlab
from the UNIX prompt. Type demo
for a brief introduction tothe capabilities of M
ATLAB
. All of the UNIX commands can be used within M
ATLAB by placing
!
in front of them. For example !ls
, lists the contents of the current directory.
M
ATLAB
is an interactive language. You can use the commandhelp
from within M
ATLAB
forhelp on M
ATLAB
commands. A set of M
ATLAB
commands can be executed by storing them in a
script
file in your M
ATLAB
path. To execute a script file, type the name of the script file (withoutthe .m
) at the M
ATLAB
prompt. To write these scripts
you will need use a UNIX editor. There areseveral editors to choose from (e.g. vi, axe, dxnotepad, emacs and pico). To start an editor fromwithin M
ATLAB
type !axe, !vi, !pico... at the M
ATLAB
command prompt or simply type theeditors name in lower case from the UNIX prompt. Alternatively, typing filebar at the M
ATLAB
prompt may simplify this task for you. Of the editors, vi is considered by many to be the hardestto use. I recommend you use dxnotepad, axe, or emacs. Once written, M-files
must be saved withthe .m
extension. If the file is simply a script
file (a list of commands that does not take inputarguments or return output variables), you can run the program by simply typing the script
namewithout the .m extension (make sure you use the right case. Its advisable to use all lower case in
naming yourM-files
. All of the student edition of the Vibration Toolbox codes are what you wouldcallfunction
files. They accept an input argument, and some return a result (output argument). Thisis functionally how commands such as sin
, cos, and plot
work. Use the command type
to view thefile vtb1_1
. You can also open it for read-only in an editor if you want to save and modify it tomake your own codes. It can be found in the directory /nfs/valhalla/users26/me/jslater/vtoolbox.
-
7/30/2019 Unix Mat Lab Intro
2/4
Sample script
file example.m
(defines 2 row vectors, multiplies them, then plots one versus theother)
1) From MATLAB prompt type !dxnotepad example.m
2) type a = [1 2 3 4 5 6];
(
;
suppresses the output to screen)3) type b = [7 8 9 10 11 12];
4) type a
(no ; this displays contents ofa
on screen)5) type b
(this displays contents ofa
on screen)6) type x = a.*b
(notice the
.
to signify array or element by element multiplication)7) type plot(a,x); (plots a
versus x
);8) type xlabel(Array a);
9) type ylabel(Array x);
10) type title(a versus x);
12) To exit the pico editor hit the ctrl
and x
key at the same time13) answer yes to save changes to example.m
14) at the M
ATLAB
prompt type example
to run the program
A function file is one that takes input arguments and returns output variables to the workspace(or another
M-file
if it was called from one). For instance, edit the file
vtb4_1.m
. You can do thisby typing:
pico /nfs/valhalla/users26/me/jslater/vtoolbox/vtb4_1.m(Dont worry, you wont be able to mess the file up). The first few lines appear as:
function [P,w,U]=vtb4_1(M,K,dispar)
%VTB4_1 Natural frequencies and eigenvectors for an undamped%system.
From the MATLAB prompt, if you type help vtb4_1 you will notice the response is thecontents of the first set of lines that start with %. This allows you to put a help section in your
program. The first line of thefunction
declares that it is a function file, the outputs are P
, w
, and U
and the inputs are M
, K
, and dispar
. Also notice that the name of the file appears in the first line.This should agree with the filename when you create your own function
files. If you want toperform the operation that vtb4_1
has been programmed to do, all you need to do is type
vtb4_1(M,K,dispar) from the M
ATLAB
prompt where M
, K
, and dispar
are variables that havealready been assigned. Alternatively, you can run thefunction by typing it as vtb4_1(1,2,3) andthe variables M, K, and dispar will take on corresponding values inside the function. (Only theoutput variables of the function will change the values of variables in the workspace. The rest ofthe variables internal to the function are local to the function.) If you want to obtain the output ofafunction, you must use output arguments in giving the command (this lets MATLAB know whereto store the information). For instance [P2,w2,U2] = vtb4_1(M,K,dispar) will execute thecommands ofvtb4_1 using the input variables M, K, and dispar . Once thefunction has completed
running, the values ofP, w, and U inside thefunction will be assigned to the variables P2 , w2 , andU2 outside thefunction in the workspace (or wherever the function was called from). In this way,functions can act as stand-alone programs inside MATLAB or as subroutines for other functionsor scripts.
-
7/30/2019 Unix Mat Lab Intro
3/4
Basic UNIX operations.
Command Action
ls List files in current directory
mv old new moves file oldto file new
cp old new copies file oldto file newrmfilename removesfilename
mkdir mydir makes a directory mydir
cd .. move up 1 directory
cd mydir move into directory mydir
rmdir mydir removes directory mydir
man topic help on topic
lps1 text prints file textto lps1 printer as text
softlist shows available software
showquota shows user disk space
exit or lo log out
^c escape/kill
^z stop (do not use this to exit MATLAB)
bg continue to run stopped job in background
jobs list the names and numbers of all jobs
%n reconnect to stopped job n
pwd show present working directory
command& run commandin the background
pine Read, edit, and send email
-
7/30/2019 Unix Mat Lab Intro
4/4
Basic MATLAB commands.
Command Actionhelp help+ Addition
- subtraction* multiplication/ division^ powera = [1 2 3] denotes vector a = [1 2 3]who shows variablesA=[1 2 ; 3 4] define matrix Ainv(A) matrix inverse of AA transpose of matrix A ([1 3 ; 2 4])plot(x,y) plots x versus y (they must have same length)xlabel(title)
assigns name title to x axis of plotylabel(title) assigns name title to y axis of plottitle(title) assigns name title to the title of plothold freezes the plot screenfigure opens a graphics windowfigure(n) selects figure number n (integer n)x = 1:5 generates x = 1 2 3 4 5 in steps of 1x = 1:.5:5 generates x from 1 to 5 in steps of .5.* multiplies two arrays element by element./ divides two arrays element by element; used at end of command to suppress output to
the screendiary creates a diary file of whats on the screen