Download - Matlab (1): The Basics
-
8/12/2019 Matlab (1): The Basics
1/24
MATLABBasics
-
8/12/2019 Matlab (1): The Basics
2/24
MATLAB User Environment
Workspace/Variable
Inspector
Command History
Command Window
-
8/12/2019 Matlab (1): The Basics
3/24
Getting help
There are several ways of getting help:
Basic help on named commands/functions is echoed to the commandwindow by:
>> help command-name
A complete help system containing full text of manuals is started by:
>> helpdesk
-
8/12/2019 Matlab (1): The Basics
4/24
System Environment
Windows MATLAB installed in c:\matlab6.5
Your codeanywhere convenient (e.g. h:\matlab)
Linux (Environment network) MATLAB installed in /apps/matlab
Your code in /home/username/matlab
Your environment configuration in ~/.matlab
-
8/12/2019 Matlab (1): The Basics
5/24
startup.m
The script $matlab_root\toolbox\local\matlabrc.mis always run at startupit reads systemenvironment variables etc, and initialisesplatform 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.mfor setting paths, forcingpreferred settings, etc.
-
8/12/2019 Matlab (1): The Basics
6/24
%-------------------------------------% Matlab startup file for IMB's laptop
%-------------------------------------
%-- add paths for my m-files --addpath d:/matlabaddpath d:/matlab/bulk2.5addpath d:/matlab/bulk2.6addpath d:/matlab/coastaladdpath d:/matlab/lidaraddpath d:/matlab/ndbc
addpath d:/matlab/pageaddpath d:/matlab/sectionsaddpath d:/matlab/sharemaddpath d:/matlab/waveletaddpath d:/matlab/LEMaddpath d:/matlab/GPSbookaddpath d:/matlab/FAAMaddpath d:/matlab/FAAM/windsaddpath d:/matlab/faam/bae
%-- add netCDF toolbox --addpath c:/matlab701/binaddpath c:/matlab701/bin/win32addpath d:/matlab/netcdfaddpath d:/matlab/netcdf/ncfilesaddpath d:/matlab/netcdf/nctypeaddpath d:/matlab/netcdf/ncutility
%-- add path for generic data --addpath d:/matlab/coastlines % coastline data
addpath d:/cw96/flight_data/jun02 % raw cw96addpath d:/cw96/flight_data/jun07 % aircraft dataaddpath d:/cw96/flight_data/jun11addpath d:/cw96/flight_data/jun12addpath d:/cw96/flight_data/jun17addpath d:/cw96/flight_data/jun19addpath d:/cw96/flight_data/jun21addpath d:/cw96/flight_data/jun23
addpath d:/cw96/flight_data/jun26addpath d:/cw96/flight_data/jun29addpath d:/cw96/flight_data/jul01
addpath d:/cw96/runs % run definitions for cw96 flights
%----------------------------------------------------------------------%-- set default figure options --set(0,'DefaultFigurePaperType','a4')
% this should be the default in EU anywayset(0,'DefaultFigurePaperUnits','inches')
% v6 defaults to cm for EU countriesset(0,'DefaultFigureRenderer','painters')
% v7 default OpenGL causes problems
Example startup.m file (for my laptop)
-
8/12/2019 Matlab (1): The Basics
7/24
addpathadds directories to
the search path. MATLAB willlook in ALL directories on thepath for:
Functions and scripts (.m files)
MATLAB data files (.mat files)
It will also look in the currentdirectory
The set commands in the
example startup.m file setsome default graphicsproperties, overriding thedefaultswill cover theselater.
-
8/12/2019 Matlab (1): The Basics
8/24
The WORKSPACE
MATLAB maintains an active workspace, anyvariables (data) loaded or defined here arealways available.
Some commands to examine workspace, movearound, etc:
>> who
Your variables are:
x y
who: lists variables in workspace
-
8/12/2019 Matlab (1): The Basics
9/24
-
8/12/2019 Matlab (1): The Basics
10/24
VARIABLES
Everything (almost) is treated as a double-precision floating point arrayby default
Typedvariables (integer, float, char,) aresupported, but usually used only for specific
applications. Not all operations are supported for alltyped variables.
[IDL uses typed variables, but allows mixing oftypes...at least to some extent]
-
8/12/2019 Matlab (1): The Basics
11/24
>> x=[1 2 3]x =
1 2 3
>> x=[1,2,3]x =
1 2 3
>> x=[1234];
>> x=[1;2;3;4]
x =1234
When defining variables, a spaceorcommaseparates elements on arow.
A newlineor semicolonforces anew row; these 2 statements areequivalent.NB. you can break definitions acrossmultiple lines.
-
8/12/2019 Matlab (1): The Basics
12/24
1 & 2D arrays are treated as formal matrices Matrix algebra works by default:
>> a=[1 2];>> b=[3
4];
>> a*bans =
11
>> b*aans =
3 64 8
1x2 row oriented array (vector)(Trailing semicolon suppresses display of output)
2x1 column oriented array
Result of matrix multiplication depends on
order of terms (non-cummutative)
-
8/12/2019 Matlab (1): The Basics
13/24
Element-by-element operation is forced bypreceding operator with .
>> a=[1 2];>> b=[3
4];
>> a.*b??? Error using ==> times
Matrix dimensions must agree. Size and shape must match
-
8/12/2019 Matlab (1): The Basics
14/24
>> a=[1 2]A =
1 2
>> b=[3 4];
>> a.*bans =
3 8
>> c=a+b
c =4 6
Matrix addition & subtraction
operate element-by-elementanyway. Dimensions ofmatrix must still match!
No trailing semicolon,immediate display of result
Element-by-elementmultiplication
-
8/12/2019 Matlab (1): The Basics
15/24
>> A = [1:3;4:6;7:9]A =
1 2 3
4 5 67 8 9
>> mean(A)ans =
4 5 6
>> sum(A)ans =
12 15 18
Most common functions operate on
columns by default
-
8/12/2019 Matlab (1): The Basics
16/24
INDEXING ARRAYS
MATLAB indexes arrays:
1 to N [row,column]
[1,1 1,2 . 1,n
2,1 2,2 . 2,n
3,1 3,2 . 3,n. . .
m,1 m,2 . m,n]
IDL indexes arrays:
0 to N-1 [column,row]
[0,0 1,0 . n-1,0
0,1 1,1 . n-1,1
0,2 1,2 . n-1,2. . . .
0,m-1 1,m-1 . n-1,m-1]
n
m
-
8/12/2019 Matlab (1): The Basics
17/24
>> A = [1:3;4:6;7:9]A =
1 2 3
4 5 67 8 9
>> A(2,3)ans =
6
>> A(1:3,2)ans =
258
>> A(2,:)ans =
4 5 6
The colon indicates a range, a:b (a to b)
A colon on its own indicates ALL values
-
8/12/2019 Matlab (1): The Basics
18/24
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
-
8/12/2019 Matlab (1): The Basics
19/24
To extract ALL the elements of an array(extracts everything to a single column vector)
>> A = [1:3; 10:10:30;
100:100:300]
A =1 2 3
10 20 30
100 200 300
>> A(:)ans =
1
101002202003
30300
-
8/12/2019 Matlab (1): The Basics
20/24
LOGICAL INDEXING
Instead of indexing arrays directly, a logicalmask can be usedan array of same size, butconsisting of 1s and 0susually derived asresult 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
-
8/12/2019 Matlab (1): The Basics
21/24
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.
-
8/12/2019 Matlab (1): The Basics
22/24
-
8/12/2019 Matlab (1): The Basics
23/24
Data is recovered with the load command
load filenameloads entire .mat file load filenamevar1 var2 loads named variables
load filename asciiloads contents of an asciiflatfile in a variable filename.
The ascii file must contain a rectangular array of numbers sothat it loads into a single matrix.
X=load(filename,-ascii)loads the ascii file into
a variable X
save var1 filename asciisaves a single variableto an ascii flat file (rectangular array of numbers)
-
8/12/2019 Matlab (1): The Basics
24/24
There have been changes to the internal format of .mat files
between MATLAB v4 and v5 (major changes to allow arrayswith 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 toan old file format withv4 andv6 switches
save filename v6