Compact Matlab Course MLC.1 15.04.2014
Matlab Command Window Workspace Command History Directories MLC.2 15.04.2014
Matlab Editor Cursor in Statement F1 Key goes to Help Information MLC.3 15.04.2014
Elementary Statements Pos. Command Explanations 1 ; Behind a statement suppress outputs 2 x = 5 x assigning the value 5 3 Continuation sign to the visual structuring of the program code 4 % Comment Comments for documentation 5 who Listing variables 6 whos Listing variables in detail 7 clear all Delete Matlab environment completely MLC.4 15.04.2014
Vectors and Matrices I Pos. Command Explanations 1 s = [6;7;9]; Producing column vector; Semicolon separate lines 2 z = [1 2 3 4 5]; Producing row vector z = [1,2,3,4,5]; 3 y = 2:5; : Operator produces row vector with elements from 2 to 5 4 y = 1:2:9; : : Operators produce row vector from 1 to 9 one increment of 2 5 A = [1,2,3; 4,5,6]; Entering matrix. Comma or blank separate new line generates elements and semicolon 6 e = A(3,4); Extracting or assigning element B(1,2)=e; [1] All numbers can be symbolic expressions which imply calculations themselves again. MLC.5 15.04.2014
Vectors and Matrices II Pos. Command Explanations 7 s = A(:,2); Column from matrix extract or assign B(:,3)=s; 8 z = A(4,:); Extracting or assigning row from matrix D(3,:)=z; 9 Z=zeros(M,N); Producing null matrix 0 10 E=eye(M); Producing identity matrix E 11 [M,N] = size(m) M,N Dimension of the matrix 12 v = s Transposition operator both for vectors and matrices MLC.6 15.04.2014
Graphics Pos. Command Explanations 1 plot(x,y); Linear x/y graph 2 loglog(x,y); double logarithmic graph 3 semilogx(x1,y1, ) single logarithmic graph for x axis 4 semilogy(x1,y1 ) single logarithmic graph for y axis 5 surfl(x,y,z) 3D graph with shading 6 shading interp Activating shading interpolation 7 colormap(gray) Using colour table gray 8 figure; Producing a new output image 9 imshow(i,[0 255]); Grey value picture with 256 grey levels (1 Byte pro Pixel) MLC.7 15.04.2014
Matlab Machdefs und Tools Pos. Command Explanations 1 MachineDefs() Computer precision etc. 2 ReadDataFileMatrix(FileNameMatrix) C++ Streaming read Matrix FileNameMatrix 3 ReadDataMatrix(fid) C++ read Streaming fid MLC.8 15.04.2014
Operators MLC.9 15.04.2014
Arithmetic Operators Operator Description + Addition - Subtraction.* Multiplication./ Right division.\ Left division + Unary plus - Unary minus : Colon operator.^ Power.' Transpose ' Complex conjugate transpose * Matrix multiplication / Matrix right division \ Matrix left division ^ Matrix power MLC.10 15.04.2014
Relational Operators Operator < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to ~= Not equal to Description MLC.11 15.04.2014
Logical Operators Operator && Description Returns logical 1 (true) if both inputs evaluate to true, and logical 0 (false) if they do not. Returns logical 1 (true) if either input, or both, evaluate to true, and logical 0 (false) if they do not Logical Operation A & B and(a, B) A B or(a, B) ~A not(a) Equivalent Function MLC.12 15.04.2014
Bit Wise Operator Operator Description Example & Returns 1 for every element location that is true (nonzero) in both arrays, and 0 for all other elements. A & B = 01001 Returns 1 for every element location that is true (nonzero) in either one or the other, or both arrays, and 0 for all other elements. A B = 11101 ~ Complements each element of the input array, A. ~A = 10010 xor Returns 1 for every element location that is true (nonzero) in only one array, and 0 for all other elements. xor(a,b) = 10100 MLC.13 15.04.2014
Control Structures MLC.14 15.04.2014
if then else if/elseif/else Execute statements if condition is true Syntax if expression statements elseif expression statements else Statements end % Respond to command-line input. % Because the input string % could be more than one character, % use strcmp rather than == to % test for equality reply = input('would you like to see an echo? (y/n): ', 's'); if strcmp(reply,'y') disp(reply) end MLC.15 15.04.2014
switch switch/case/otherwise Switch among several cases based on expression Syntax switch switch_expression case case_expression statements case case_expression statements : otherwise statements end % Conditionally display different text % depending on a value entered at % the command line mynumber = input('enter a number:'); switch mynumber case -1 disp('negative one'); case 0 disp('zero'); case 1 disp('positive one'); otherwise disp('other value'); end MLC.16 15.04.2014
for For Execute statements specified number of times Syntax for index = initval:step:endval program statements : end % Step by increments of -0.1, and display the step values for s = 1.0: -0.1: 0.0 disp(s) end MLC.17 15.04.2014
while while Repeatedly execute statements while condition is true Syntax while expression statements end % Find the first integer n for which factorial(n) is a 100-digit number n = 1; nfactorial = 1; while nfactorial < 1e100 n = n + 1; nfactorial = nfactorial * n; end MLC.18 15.04.2014
Debugging with Matlab MLC.19 15.04.2014
Debugging I Cursor in Line F12 Key sets and delete Break Point Go to Main Program Activate Run or activate Main Program via the Matlab command line MLC.20 15.04.2014
Debugging II Breakpoints Continue / F5 Step / F10 Step In / F11 Step Out / Shift + F11 Run to Cursor MLC.21 15.04.2014
Debugging III Step / F10 Cursor to theta1 shows the value MLC.22 15.04.2014
END MLC.23 15.04.2014