Sunteți pe pagina 1din 24

MATLAB

Basics
MATLAB User Environment

Workspace/Variable
Inspector

Command Window

Command History
Getting help
There are several ways of getting help:

Basic help on named commands/functions is echoed to the command


window by:

>> help command-name

A complete help system containing full text of manuals is started by:

>> helpdesk
System Environment
• Windows
– MATLAB installed in c:\matlab6.5
– Your code…anywhere convenient (e.g. h:\matlab)

• Linux (Environment network)


– MATLAB installed in /apps/matlab
– Your code in /home/username/matlab
– Your environment configuration in ~/.matlab
startup.m
• The script $matlab_root\toolbox\local\matlabrc.m
is always run at startup – it reads system
environment variables etc, and initialises
platform dependent settings. If present it will run
a user defined initialisation script: startup.m
– Linux: /home/user/matlab/startup.m
– Windows: $matlab_root\toolbox\local\startup.m

• Use startup.m for setting paths, forcing


preferred settings, etc.
Example startup.m file (for my laptop)
%------------------------------------- %-- add path for generic data --
% Matlab startup file for IMB's laptop addpath d:/matlab/coastlines % coastline data
%-------------------------------------
addpath d:/cw96/flight_data/jun02 % raw cw96
%-- add paths for my m-files -- addpath d:/cw96/flight_data/jun07 % aircraft data
addpath d:/matlab addpath d:/cw96/flight_data/jun11
addpath d:/matlab/bulk2.5 addpath d:/cw96/flight_data/jun12
addpath d:/matlab/bulk2.6 addpath d:/cw96/flight_data/jun17
addpath d:/matlab/coastal addpath d:/cw96/flight_data/jun19
addpath d:/matlab/lidar addpath d:/cw96/flight_data/jun21
addpath d:/matlab/ndbc addpath d:/cw96/flight_data/jun23
addpath d:/matlab/page addpath d:/cw96/flight_data/jun26
addpath d:/matlab/sections addpath d:/cw96/flight_data/jun29
addpath d:/matlab/sharem addpath d:/cw96/flight_data/jul01
addpath d:/matlab/wavelet
addpath d:/matlab/LEM addpath d:/cw96/runs % run definitions for cw96 flights
addpath d:/matlab/GPSbook
addpath d:/matlab/FAAM %----------------------------------------------------------------------
addpath d:/matlab/FAAM/winds %-- set default figure options --
addpath d:/matlab/faam/bae set(0,'DefaultFigurePaperType','a4')
% this should be the default in EU anyway
%-- add netCDF toolbox -- set(0,'DefaultFigurePaperUnits','inches')
addpath c:/matlab701/bin % v6 defaults to cm for EU countries
addpath c:/matlab701/bin/win32 set(0,'DefaultFigureRenderer','painters')
addpath d:/matlab/netcdf % v7 default OpenGL causes problems
addpath d:/matlab/netcdf/ncfiles
addpath d:/matlab/netcdf/nctype
addpath d:/matlab/netcdf/ncutility
• addpath – adds directories to • The ‘set’ commands in the
the search path. MATLAB will example startup.m file set
look in ALL directories on the some default graphics
path for: properties, overriding the
– Functions and scripts (.m files) defaults – will cover these
– MATLAB data files (.mat files) later.
• It will also look in the current
directory
The WORKSPACE
• MATLAB maintains an active workspace, any
variables (data) loaded or defined here are
always available.
• Some commands to examine workspace, move
around, etc:

who : lists variables in workspace


>> who

Your variables are:

x y
whos : lists names and basic properties of variables in the workspace
>> whos
Name Size Bytes Class

x 3x1 24 double array


y 3x2 48 double array

Grand total is 9 elements using 72 bytes

pwd, cd, dir, ls : similar to operating system (but no option switches)


>> pwd
ans =

D:\

>> cd cw96\jun02
>> dir
. 30m_wtv.mat edson2km.mat jun02_30m_runs.mat

.. 960602_sst.mat edson_2km_bulk.mat
VARIABLES
• Everything (almost) is treated as a double-
precision floating point array by default
– Typed variables (integer, float, char,…) are
supported, but usually used only for specific
applications. Not all operations are supported for all
typed variables.
– [IDL uses typed variables, but allows mixing of
types...at least to some extent]
>> x=[1 2 3] When defining variables, a space or
x = comma separates elements on a
1 2 3 row.

>> x=[1,2,3]
x =
1 2 3

>> x=[1 A newline or semicolon forces a


2 new row; these 2 statements are
3 equivalent.
4]; NB. you can break definitions across
>> x=[1;2;3;4] multiple lines.

x =
1
2
3
4
• 1 & 2D arrays are treated as formal matrices
– Matrix algebra works by default:

>> a=[1 2]; 1x2 row oriented array (vector)


>> b=[3 (Trailing semicolon suppresses display of output)
4];
2x1 column oriented array
>> a*b
ans =
11
Result of matrix multiplication depends on
>> b*a order of terms (non-cummutative)
ans =
3 6
4 8
• Element-by-element operation is forced by
preceding operator with ‘.’
>> a=[1 2];
>> b=[3
4];

>> a.*b
??? Error using ==> times
Size and shape must match
Matrix dimensions must agree.
>> a=[1 2] No trailing semicolon,
A = immediate display of result
1 2

>> b=[3 4];

>> a.*b Element-by-element


ans = multiplication
3 8

>> c=a+b Matrix addition & subtraction


c = operate element-by-element
4 6 anyway. Dimensions of
matrix must still match!
>> A = [1:3;4:6;7:9]
A =
1 2 3 Most common functions operate on
4 5 6 columns by default
7 8 9

>> mean(A)
ans =
4 5 6

>> sum(A)
ans =
12 15 18
INDEXING ARRAYS
n

• MATLAB indexes arrays: • IDL indexes arrays:


– 1 to N – 0 to N-1
– [row,column] – [column,row]

[1,1 1,2 . 1,n [0,0 1,0 . n-1,0


2,1 2,2 . 2,n 0,1 1,1 . n-1,1
0,2 1,2 . n-1,2
3,1 3,2 . 3,n
. . . .
. . .
0,m-1 1,m-1 . n-1,m-1]
m,1 m,2 . m,n]
>> A = [1:3;4:6;7:9]
A =
1 2 3
4 5 6
7 8 9

>> A(2,3)
ans =
6

>> A(1:3,2) The colon indicates a range, a:b (a to b)


ans =
2
5
8

>> A(2,:) A colon on its own indicates ALL values


ans =
4 5 6
THE COLON OPERATOR
• Colon operator occurs in several forms
– To indicate a range (as above)
– To indicate a range with non-unit increment
>> N = 5:10:35
N =
5 15 25 35
>> P = [1:3; 30:-10:10]
P =
1 2 3
30 20 10
• To extract ALL the elements of an array
(extracts everything to a single column vector)
>> A = [1:3; 10:10:30; >> A(:)
ans =
100:100:300]
1
A = 10
1 2 3 100
2
10 20 30
20
100 200 300 200
3
30
300
LOGICAL INDEXING
• Instead of indexing arrays directly, a logical
mask can be used – an array of same size, but
consisting of 1s and 0s – usually derived as
result of a logical expression.
>> X = [1:10]
X =
1 2 3 4 5 6 7 8 9 10
>> ii = X>6
ii =
0 0 0 0 0 0 1 1 1 1
>> X(ii)
ans =
7 8 9 10
Basic Operators
+, -, *, / : basic numeric operators
\ : left division (matrix division)
^ : raise to power
’ : transpose (of matrix) – flip along diagonal

• fliplr(), flipud() : flip matrix about


vertical and horizontal axes.
SAVING DATA
• MATLAB uses its own platform independent file format
for saving data – files have a .mat extension
– The ‘save’ command saves variables from the
workspace to a named file (or matlab.mat if no
filename given)
• save filename – saves entire workspace to filename.mat
• save var1 var2 … filename – saves named variables
to filename.mat

– By default save overwrites an existing file of the same


name, the –append switch forces appending data to
an existing file (but variables of same name will be
overwritten!)
• save var1 var2 filename -append
– Data is recovered with the ‘load’ command
• load filename – loads entire .mat file
• load filename var1 var2 …– loads named variables

• load filename –ascii – loads contents of an ascii


flatfile in a variable ‘filename’.

The ascii file must contain a rectangular array of numbers so


that it loads into a single matrix.

• X=load(‘filename’,’-ascii’) – loads the ascii file into


a variable ‘X’

• save var1 filename –ascii – saves a single variable


to an ascii flat file (rectangular array of numbers)
• There have been changes to the internal format of .mat files
between MATLAB v4 and v5 (major changes to allow arrays
with more than 2 dimensions, structures, cell arrays…), and
again with v7 (minor change to use unicode instead of ascii).
Later versions will load old format files. You can force save to
an old file format with –v4 and –v6 switches

save filename –v6

S-ar putea să vă placă și