Warning

This page is located in archive.
Go to the latest version of this course pages.
Go the latest version of this page.

If some functions of your submitted work are in sub-directories, the Matlab path must be set at the beginning of your code, e.g. like this

if( ~exist( 'figexport', 'file' ) ) addpath tools end if( ~exist( 'my_specific_function', 'file' ) ) addpath my_library end

- Do not trust any results unless you verified them.
- Always verify your results.
- Try to visualize your results.
- Each Matlab command has help. E.g. for
`plot`

help plot % text version doc plot % html version

Matlab has a powerful debugger. Either put breakpoints everywhere you want, or use the command `keyboard`

in your code to stop processing at the place. Exit the debugging mode:

dbquit

You can also start debugging automatically in case of error by turning on

dbstop error

Especially when some user input is needed, it is wise to store intermediate data for later reuse. Following example could be helpful

if( ~exist( 'my_points.mat', 'file' ) ) [x y] = ginput( 7 ); save( 'my_points.mat', 'x', 'y' ); else load( 'my_points.mat' ); end

Vectors are stored as column matrices.

u = [1;2]; X = [1;2;3]; Xmore = [1 2 3; 4 5 6]'; % transpose at the end

Vectors are multiplied by a matrices on the left:

P = [1 0 0 -5; 0 1 0 -6; 0 0 1 1]; % a camera ux_homog = P * [X;1]; % projection

I = eye(3); % 3x3 identity I2 = diag( [1 1 1] ); % same result Z = zeros(3); % 3x3 matrix of zeroes O = ones(3); % 3x3 matrix of ones

X1 = [1;2;3]; X2 = [4;5;6]; d2 = norm(X1-X2); % cannot be used for more vectors d2 = sqrt(sum((X1-X2).^2)); % for matrices with points stored in columns

u1 = [4;5;1]; u2 = [7;8;1]; dot12 = u1' * u2; % apostrophe is the transpose

In addition to standard matrix addition and subtraction, there are some operators working element-by-element.

M1 = [1 2; 3 4; 5 6]; M2 = [7 8; 9 10; 11 12]; % note the sizes A = M1 .* M2; % Hadamard product (multiplication element-by-element; note the dot) B = M1 ./ M2; % Hadamard division (division element-by-element) C = M1 .^ 2; % power element-by-element

numbers = [1 2 sqrt(-3) acos(2) 4 6+3i 7 ]; is_real = imag( numbers ) == 0; % logical array real_only = numbers( is_real );

u1 = [4;5;1]; u2 = [7;8;1]; u12x = cross(u1,u2); % verification of orthogonality by dot product % (remember the recommendations 1 and 2) u12x' * u1 % should be zero u12x' * u2 % should be zero

Let x = r cos(a) and y = r sin(a). Then the angle can be recovered as

a = atan2( y, x );

This function is safe when either of its arguments is zero (but not both), and works for four quadrants.

Let `hnd`

be some axes handle (e.g. `gca`

) or some figure handle (e.g. `gcf`

). The snapshot identical to the (pixel-wise) appearance of the screen is captured as

f = getframe( hnd ); img = f.cdata;

Notes: when capturing a sequence, do not forget to call `drawnow`

before capturing. Also be careful not to obscure the captured window by other GUI content, mouse pointer, etc.

mov = avifile( 'my_file.avi' ); mov.compression = 'none'; mov.fps = 25; % or whatever you want % now iterate creation of frames % all frames must have the same size mov = addframe( mov, img ); % end of frame creation mov = close( mov );

Note that video compression (compression different from 'none') usually does not work well, so preferred way is to store raw sequence and compress later.

courses/gvg/labs/hw-help-matlab.txt · Last modified: 2020/03/26 21:26 by xmatousm