24.01.2015 Views

Nuts & Bolts guide to MATLAB

Nuts & Bolts guide to MATLAB

Nuts & Bolts guide to MATLAB

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Nuts</strong> & <strong>Bolts</strong> <strong>guide</strong> <strong>to</strong><br />

<strong>MATLAB</strong><br />

Adrian KC Lee ScD<br />

MGH-HST Athinoula A. Martinos Center for Biomedical Imaging;<br />

Department of Radiology, Harvard Medical School, Bos<strong>to</strong>n, MA.<br />

November 18 2010 | Why.N.How Tu<strong>to</strong>rial Series<br />

Friday, November 26, 2010<br />

1


Overview<br />

‣ Why do we use <strong>MATLAB</strong><br />

‣ Navigating in <strong>MATLAB</strong> environment.<br />

‣ Matrix Operations; Cell array; Structure.<br />

‣ General programming tips.<br />

‣ Simple <strong>MATLAB</strong> functions.<br />

‣ Error Flow / Debugging mode.<br />

‣ Plots / Graphs.<br />

Friday, November 26, 2010<br />

2


Why <strong>MATLAB</strong><br />

‣ <strong>MATLAB</strong> = MATrix LABora<strong>to</strong>ry<br />

• Basic data element is the matrix;<br />

• Vec<strong>to</strong>rized operations;<br />

• Good for generating engineering graphics.<br />

‣ Specialized Toolboxes.<br />

‣ Interacts with Inputs and Output devices.<br />

Friday, November 26, 2010<br />

3


<strong>MATLAB</strong> environment<br />

Current direc<strong>to</strong>ry<br />

Variables currently<br />

in Workspace<br />

Command prompt<br />

Command His<strong>to</strong>ry<br />

Friday, November 26, 2010<br />

4


Matrix Manipulations<br />

Review.m -<br />

Matrix Manipulations (1)<br />

‣ Matrix size: row x column<br />

• Basic data element is the matrix;<br />

‣ Vec<strong>to</strong>rs: a degenerate matrix<br />

• Row Vec<strong>to</strong>r = matrix with only 1 row<br />

- Separa<strong>to</strong>r used: , or <br />

• Column Vec<strong>to</strong>r = matrix with only 1 column<br />

- Separa<strong>to</strong>r used: ; or <br />

Friday, November 26, 2010<br />

5


Matrix Manipulations (2)<br />

‣ Element in matrix can be accessed:<br />

• A(row, column) = new_value;<br />

‣ Matrix Transponse<br />

• e.g., A’ (Note: Hermitian conjugate for Complex Field)<br />

- Separa<strong>to</strong>r used: , or <br />

‣ Sub-matricies:<br />

• Range specifies by :<br />

• Entire dimension selected by :<br />

Review.m -<br />

Matrix Manipulations (2)<br />

Friday, November 26, 2010<br />

6


Matrix Manipulations (3)<br />

‣ Math functions operate on matrices:<br />

Review.m -<br />

Matrix Manipulations (3)<br />

• Beware of commands used<br />

- e.g., * vs .* (matrix vs array multiply)<br />

• Reminder: Matrices are not commutative in<br />

multiplication, i.e., A*B ≠B*A<br />

‣ Solving system of linear equations:<br />

• Ax = b x = inv(A)*b or x = A\b<br />

‣ More math functions...<br />

Friday, November 26, 2010<br />

7


Array vs. Cell<br />

‣ Array: dimensions and types are consistent<br />

B_array = [‘abc,’,’d’;’ef’,’gh’];<br />

B_error = [‘abc’;‘defgh’];<br />

B_mix = [50,‘a’;’c’,70]<br />

char(50) = “2” and char(70) = “F”<br />

B_cell = {50,‘a’;’c’,70}<br />

=<br />

[50] ‘a’<br />

’c’ [70]<br />

Friday, November 26, 2010<br />

8


Array of Structures<br />

‣ Different data fields for each subject<br />

data = {‘KC’, 30, 10; ...<br />

‘Adrian’,25,23; ...<br />

‘Lee’,60,50};<br />

NAME = data(:,1)<br />

AGE = cell2mat(data(:,2))<br />

‣ Array of structures<br />

subject(1).name = 'KC';<br />

subject(1).age = 30;<br />

subject(1).mental_age = 10;<br />

subject(1).HDR = S01;<br />

subject(2).name = 'AA';<br />

subject(2).age = 25;<br />

subject(2).mental_age = 23;<br />

subject(2).HDR = S02;<br />

Warning: only an illustration. Not HIPPA kosher!<br />

Friday, November 26, 2010<br />

9


Control Statements<br />

‣ Conditional:<br />

Review.m -<br />

Control statements<br />

• Syntax<br />

if <br />

<br />

(elseif <br />

)<br />

else<br />

<br />

end<br />

Friday, November 26, 2010<br />

10


Control Statements (2)<br />

‣ Repetition:<br />

Review.m -<br />

Control statements<br />

• Looping with while<br />

while <br />

<br />

end<br />

• Looping with for<br />

for var = start:step:end<br />

<br />

end<br />

Friday, November 26, 2010<br />

11


General Computing Tips<br />

‣ Divide and Conquer.<br />

‣ Comments (both header and body).<br />

‣ Choose obvious (and not reserved) variable names.<br />

‣ Keep trace of different editions.<br />

‣ Concept of “Flags.”<br />

‣ Control flow of program (error messages).<br />

Friday, November 26, 2010<br />

12


<strong>MATLAB</strong> functions<br />

‣ Function declaration:<br />

fac<strong>to</strong>rial(2).m, comb(2).m<br />

function (output args) = ...<br />

(input args)<br />

‣ Save as an m-file (.m) using the exact function name<br />

for the filename.<br />

‣ Make sure that the path is added <strong>to</strong> the workspace<br />

<strong>to</strong> call function.<br />

Friday, November 26, 2010<br />

13


DEBUG mode<br />

‣ Debug > S<strong>to</strong>p if Errors / Warnings ><br />

• Access <strong>to</strong> variables in workspace<br />

• Step / Step In / Step Out <strong>to</strong> trace loop<br />

• (if no <strong>MATLAB</strong> errors, make one yourself <strong>to</strong> have<br />

access <strong>to</strong> DEBUG mode for tracing)<br />

• Use dbquit <strong>to</strong> exit debug mode<br />

Friday, November 26, 2010<br />

14


<strong>MATLAB</strong> plots<br />

‣ Basic 2-D <strong>MATLAB</strong> plotting command<br />

• plot(x,y,s)<br />

• x and y must be the same size<br />

• s is a string that can be used <strong>to</strong> denote color,<br />

symbol and line-type of the plot.<br />

• See also semilogx, semilogy, loglog,<br />

polar, fill, bar, errorbar, hist,<br />

plotyy, area, pie, stem, stairs etc.<br />

Friday, November 26, 2010<br />

15


Plotting (time domain)<br />

plot_example.m<br />

‣ Define time vec<strong>to</strong>rs <strong>to</strong> generate <strong>to</strong>nes and plots:<br />

• Syntax: start:step:finish<br />

• close all hidden, hold<br />

• figure creates a new figure.<br />

• subplot, xlabel, ylabel, title, axis,<br />

strcat, stem<br />

Friday, November 26, 2010<br />

16


References & other tips<br />

‣ www.mathwork.com/matlabcentral<br />

• File Exchange<br />

- Shared programs (e.g., errorbarxy)<br />

• <strong>MATLAB</strong> helpdesk<br />

‣ Linear Algebra Tu<strong>to</strong>rial<br />

• MIT OCW 18.06 Linear Algebra<br />

Friday, November 26, 2010<br />

17

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!