0% found this document useful (0 votes)
85 views3 pages

Basic MATLAB Commands

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

ME 4053 MECHANICAL SYSTEMS LABORATORY

Basic MATLAB Commands


This section is a quick guide to Matlab commands used most commonly in ME 4053. Included in this section are
the basics of subscripting a variable, loading data files, plotting data in Matlab and saving Matlab variables. A
more complete introduction to using MATLAB for ME 4053 is contained in the CD included with this
manual. It is strongly recommended that you review the information contained on the CD. Both the
Matlab Users Guide and Reference Guide are available in the ME 4053 lab for more information on these and
other topics. The ME 4053 TA's should also be able to answer your Matlab questions.

Introduction:

Matlab is a computer language which is designed to efficiently handle matrix algebra. All variables in Matlab are
treated as an NxM matrix by the program. N = number of rows. M = number of columns.

Subscripting:

Often all of the data stored in a Matlab variable is not needed to perform some task such as scaling or plotting. In
order to select the appropriate data from a Matlab variable, subscripts which follow the variable name must be
used. The subscripts are enclosed in parenthesis ( ) and the ranges of row and column data must be separated by
a comma ( , ).

The row and column data ranges are denoted in three ways:

1) an integer, which tells which row or column is needed

2) two integers separated by a colon ( : ), which give the first and last rows or columns to take data from

3) a lone colon ( : ), which denotes all of the rows or columns

Examples of Subscripting (for these examples let A by a 10 X 10 matrix of data)

A(2,5) = the data point located at the intersection of row 2 and column 5 A(1:6, 4) = a 6 X 1 vector of the data in
rows 1 to 6 of column 4 A(2, 3:8) = a 1 X 6 vector of the data in columns 3 to 8 of row 2 A(1:5, 4:10) = a 5 X 7
submatrix of A with the data from rows 1 to 5 contained in columns 4 to 10 A(: , 3) = a 10 X 1 vector of all the
data in column 3 A(7 , :) = a 1 X 10 vector of all the data in row 7

Transposing a Matrix:

Some Matlab functions only accept data in a column vector format and it is often necessary to transpose a row
vector into a column vector before the data can be used. Transposing a matrix is also needed when multiplying
two matrices together. To transpose a matrix, the variable name is followed by an apostrophe (`).

Example: Let A be a 3 X 5 matrix B = A' yields B = the 5 X 3 transpose of A [Warning: For complex data ()
gives the complex conjugate transpose, use . to avoid this problem]

- 17 - 7/30/03
ME 4053 MECHANICAL SYSTEMS LABORATORY

Loading Data into Matlab

In this lab you will be using Matlab to analyze data created by other programs. This data must first be loaded into
the Matlab workspace. Also data you have created in Matlab can be saved (see Saving Data in Matlab) and
reloaded into Matlab.

To load data into Matlab the load command is used. The basic format of the command is the load statement
followed by the filename with extension. If the file being loaded was created by Matlab (.mat extension) the data
will be loaded under the variable names stored in the file. If the file being loaded is an ASCII file from another
program the data will be loaded into a variable name which is the filename without any extension.

NOTE: Do not use ASCII filenames which begin with numbers for data you want to load into Matlab, it just
doesn't work.

Examples: Let `matfile.mat' be a Matlab generated file with data stored under the variables A and B and let
`test.dat' be an ASCII data file generated by some other program.

load matfile.mat Loads the data in matfile.mat under the variables A and B

load test.dat Loads the data in test. dat under the variable test

Note: Generally you will have to include the complete file location and name for Matlab to find the file
you need. For example, in the above commands test.dat may be c: Lstudentslth8ltest.dat

Saving Data in Matlab

Data generated in Matlab can be saved to either the hard drive or a floppy from Matlab. The basic command
format is the save command followed by the name of the file to create. This is followed by a list of the variables
which are to be saved in the file. To create an ASCII file which can be read by a spreadsheet the list of variables
is followed by the command -ascii

Examples: Let A and B be Matlab variables which need to be saved

save c:\students\t8\matfile A B Saves the data in A and Bin the file matfile.mat in the directory given
save a:\test.dat A B ascii Saves A and B in ASCII format to a floppy in a file called
test. dat

Printing out Plots

Each of the workstations in the ME 4053 lab is connected to a laser printer. To use this printer to get a hard copy
of your plots, first make the plot the active window on the screen. Then select `File' from the main menu and
`Print' from the submenu. The printer will make a full size 8.5" x 11" printout of the plot window.

If you would like to save your plot so that it can be included in your lab reports or to print it on a laser printer
select `Edit' from the main menu of the plot window. Then select `Copy to Bitmap'. This will copy the plot
window onto the Windows

- 18 - 7/30/03
ME 4053 MECHANICAL SYSTEMS LABORATORY

Clipboard Editor located in the Main folder of Windows. To save the Bitmap of the plot open the Clipboard
Editor and select `File' and then `Save As' to give the plot a filename. You should then be able to import the plot
into any Windows program.

WHOS Command

The "whos" command displays a list of all the variables in the Matlab Workspace along with their size and the
number of rows and columns.
Using the Arrow Keys / Editing Commands

Matlab remembers every command you type into the program. A listing of previous commands can be seen by
using the up and down arrows to scroll the previous commands onto the current command line. This is very
useful in reducing the amount of typing you must do. For example, if you need to load several ASCII files into
Matlab you can type in the complete load command for the first file and then use the up arrow key to retrieve the
load command. Then all you have to do is edit the file name to load the next file.

In addition, the usual Windows edition technique of cutting and pasting using the <CTRL> C and <CTRL> V
key strokes can be used to insert text into the active Matlab command line.

- 19 - 7/30/03

You might also like