Presentation is loading. Please wait.

Presentation is loading. Please wait.

Getting Started with MATLAB

Similar presentations


Presentation on theme: "Getting Started with MATLAB"— Presentation transcript:

1 Getting Started with MATLAB
Benjamin Drozdenko Northeastern U. Campus-Wide MathWorks TA Graduate Research Assistant, Ph.D. Candidate February 13, 2016

2 Tutorial Outline Intro & MATLAB Desktop Windows (15 min)
MATLAB Language Command Syntax (15 min) Finding Help and Documentation (5 min) Creating Scripts and Functions (5 min) Running Code (5 min) Setting Breakpoints and Debugging (5 min) Additional Topics, as time permits (<10 min) Programming Best Practices Global Variables Matrix Manipulation This tutorial, “Getting Started with MATLAB”, is intended to take 60 minutes, with a 5-minute period at the end for questions.

3 MathWorks Teaching Assistant
Need Help with MATLAB or Simulink? Then contact the MathWorks Teaching Assistant, Ben Drozdenko, a Ph.D. candidate and former MathWorks employee. For personalized, immediate on-campus assistance, please attend my Spring 2016 Office Hours: Snell Library, 1st Flr, Rm 138B Mondays: 1:30-5:30 pm Wednesdays: 9 am-12 pm Fridays: 12 pm-5 pm To find the office, enter Snell Library and go down the hallway to the left of the information desk. At the end of the hallway, turn left at the printing station. Office hours are in the smaller study room, the “Bullpen”, room 138B. For general or public MATLAB & Simulink questions, please visit my Blackboard Site: MATLAB Help & Mentoring For specific or private MATLAB & Simulink questions, please send me an

4 Online MATLAB Tutorials & Info
MATLAB On-Ramp: A free, 2-hour, interactive, self-paced course to give you a MATLAB overview: On-Campus MATLAB Seminars: MATLAB & Simulink Tutorial for Students: mathworks.com/academia/student_center/tutorials To post questions about existing MathWorks tools & functions, try MATLAB Answers: mathworks.com/matlabcentral/answers To see if anyone has already created MATLAB functions or Simulink models for some specific purpose, check the MATLAB Central File Exchange: mathworks.com/matlabcentral/fileexchange

5 Data Science Resources
Data Science Recorded Webinars & Videos: mathworks.com/solutions/data-analytics/ Related Discovery & Solutions Pages: mathworks.com/discovery/big-data-matlab.html mathworks.com/solutions/machine-learning/ Relevant Webinars: mathworks.com/videos/data-analytics-with-matlab html mathworks.com/videos/machine-learning-with-matlab html   mathworks.com/videos/signal-processing-and-machine-learning-techniques-for-sensor-data-analytics html  

6 To Install MATLAB For students to install MATLAB on your personal computer, see: Go to mathworks.com and create a user account using using your husky.neu.edu address. After creating a user account, associate the account with Northeastern University’s Student TAH License #: Go to the License Center page, click on Add License in the upper right corner, select Associate by Activation Key, and click Continue. When prompted, enter the Activation Key: Go to the Downloads page and click Download for the current release (R2015b as of 10/6/2015) to download the installer. Once it’s downloaded, run the executable and follow the installation instructions.

7 MATLAB Desktop Windows (1)
Toolstrip: Perform common operations, such as new, save, import data, set preferences, and toggle windows (layout) Current Folder: See & modify files in your working directory Command Window: Where you enter in MATLAB® programming language commands MATLAB stands for “Matrix Laboratory”. In recent usage, it can refer to one of two things, either (A) the MATLAB program, which brings up the MATLAB Desktop, a graphical user interface (GUI) for the Windows, Mac, and Linux operating systems, or (B) the MATLAB programming language, a means of defining code syntax. First, we focus on (A), the GUI, alternatively known as the MATLAB Desktop. Workspace: See variables you’ve created

8 MATLAB Desktop Windows (2)
Click the Layout button in the Toolstrip to toggle Desktop Windows Command History browser: displays all past commands you entered; double-click to run again

9 MATLAB Desktop Windows (3)
Click Import Data Select Excel spreadsheet file Select range of cells Click Import Selection Check Workspace Select a specific range of cells that you want to import. To start with, focus on just numeric data. The MATLAB GUI allows you to perform operations in one of two ways (1) graphically, using buttons, apps, and wizards launched from the MATLAB Desktop, or (2) programmatically, by entering in MATLAB commands. Before we get into details of the MATLAB language syntax, try importing data from an Excel spreadsheet by clicking the “Import Data” button in the MATLAB toolstrip.

10 MATLAB Language Command Syntax (1): Matrices, Scalars, and Vectors
>> [assignment] [expression] [;] (opt: ans default) (opt: suppress output) >> *5+9/3-2^3 >> a = 5*5+9/3-2^3 >> b = 1:10 >> c = 1:1e6; c2 = 2*c+1; >> d = 1:2:10; e = 10:-2:2; >> f = d'; g = d.*e+1; >> h = 'A custom string'; >> i = [1,2,3]; j = [4;5;6]; >> k = [1, 2, 3; 4, 5, 6]; >> l = k'; MATLAB commands are always in the form [assignment][expression][;], where the assignment and semi-colon (;) are optional. If the assignment is left off, MATLAB creates a variable “ans” in the workspace to hold the result of the expression. If the semi-colon is left off, the result of the assignment is printed in the command window. In MATLAB, every numeric variable you create is by default a matrix. A scalar is a special type of matrix with size 1x1, meaning 1 row and 1 column. You can use A row vector is a matrix with size 1xm and a column vector is a matrix with size nx1. Use the colon (:) operator to create a row vector that increments by 1. Use two colons to specify an increment, or step size, which can be positive or negative. The apostrophe (‘) performs a transpose operation, turning a row vector into a column vector and vice-versa. To perform a vector dot-product, use the “.*” operator. Use the square brackets to manually construct a vector or 2x2 matrix. Within the square

11 >> [assignment] [expression] [;]
MATLAB Language Command Syntax (2): Parentheses, Square Brackets, and Curly Braces >> [assignment] [expression] [;] (opt: ans default) (opt: suppress output) >> m = [d, e]; n = [d; e]; >> o = 2*k+1; p = k + o; >> q = [k, o]; r = [o; k]; >> s = m(6); t = n(7); >> u = q(2,5); v = r(:,2); >> w = h(3:8); >> y x.^2 + 2*x + 1; >> z = {w, u; y, a}; >> z2 = z(2,:); >> za = z{2,2};

12 >> [assignment][expression] [;]
MATLAB Language Command Syntax (3): Calling functions, multiple inputs and outputs >> [assignment][expression] [;] (opt: default ans) (opt: suppress output) >> r1x1 = rand >> r2x2 = rand(2) >> r2x4 = rand(2,4); >> sig = audioread('whale.ogg'); >> [sig, fs] = audioread('whale.ogg'); >> [nr, nc] = size(sig); >> sz = size(sig); >> img = imread('whale.jpg'); >> num = xlsread('Al.xlsx'); >> [num, txt] = xlsread('Al.xlsx',1);

13 MATLAB Language Command Syntax (4): Keywords for Control
>> iskeyword ans=break,case,catch,classdef,continue,else,elseif,end,for,function,global,if,otherwise, parfor,persistent,return,spmd,switch,try,while >> a=0; >> while a < 10 % condition to loop >> for n = 1:10 % vector iterator values >> if n == 3 % condition to execute >> a = a+n; >> disp(a); >> end % terminates IF block >> end % terminates FOR loop block >> end % terminates WHILE loop block

14 Finding Help and Documentation (1)
To open the MATLAB Help Browser Window: 1.) Click “Help” Or 2.) At Command Prompt, enter: >> doc Or 3.) In a web browser, open the documentation online at:

15 Finding Help and Documentation (2)
To get information about MATLAB operators or a specific function: 1.) Right-click a function name in your command, and select “Help on Selection” Or 2.) At Command Prompt, enter: >> doc ops >> doc rand >> help rand

16 Finding Help and Documentation (3)
To search the documentation for a specific topic: 1.) Enter in a search term (e.g. “read xlsx”) in the search bar at the top of the Help browser Or 2.) At Command Prompt, enter: >> docsearch read xlsx

17 Creating Scripts and Functions (1)
To create a new MATLAB program file: 1.) Click “New Script” Or 2.) At the Command Prompt, enter: >> edit Or 3.) Select lines from Command History, right-click, and select “Create Script”

18 Creating Scripts and Functions (2)
To open an existing MATLAB program file: 1.) Click Open and select file name 2.) At the Command Prompt, enter: >> edit readAls

19 Creating Scripts and Functions (3)
>> edit readAl To turn a script into a function, put a function declaration on the 1st line of your MATLAB program file in the form: [outputs] = functionName(inputs) The inputs should be used and the outputs must be assigned within the program code.

20 Running Code To run a MATLAB program file: 1.) Click “Run”
Or 2.) At the Command Prompt, enter the file name & any inputs: >> readAls >> eq=readAl( 'Al.xlsx');

21 Setting Breakpoints and Debugging (1)
When your MATLAB program code is poorly written, you may receive an error. To find the source of the error, go to the line where the error occurred and set a breakpoint.

22 Setting Breakpoints and Debugging (2)
>> eq=readAle('Al.xlsx'); Click on the dash(—) next to line 43 to set a breakpoint. Next, run the program again and it will stop execution at the error line. The green arrow indicates the line about to be executed. Mouse over variables to see their values and sizes, or view the Workspace browser. Can you find the source of the error in readAle.m? Next, run the program again and it will stop execution at the error line. Green arrow represents line about to be executed. Mouse over variables to see their values and sizes, or view the Workspace browser. Can you find the source of the error?

23 Additional Topics (1): Programming Best Practices
Use functions instead of scripts when possible Terminate each function with end keyword Check the Code Analyzer for suggestions: Vectorize your code: Avoid for/while/if blocks when possible t=linspace(-10,10); [X,Y]=meshgrid(t,t); Z=X.^2+Y.^2; surf(X,Y,Z); Preallocate memory for arrays that increase in size y = zeros(n,1); for k=1:1e6, y(k)=child(x(k)); end Avoid global variables Pass variables via function arguments instead

24 Additional Topics (2): Global Variables
Must be defined using the keyword global wherever used function out=parent(in) global g; out = g*child(in); end function out=child(in) global g; out = in.^2+g; end Use “clear global g” to delete global variable g. Alternatives: Use nested functions or function handles instead function out=parent(in) g=5; x.^2+g; out = g*child(in); end function out=parent(in) g=5; function out=child(in) out = in.^2+g; end out = g*child(in);

25 Additional Topics (3): Matrix Manipulation
For matrices, use *,/,\,^ for matrix operations and .*,./,.^ for array (element-wise) operations. A = randi(4,3); B=A*A', C=A.*A Common math functions (e.g. mean, sum) operate on each column of a matrix independently. Set the dim argument to operate on a different dimension or index by (:) to get the overall mean. A1 = randi(4,3); m1 = mean(A1) m2 = mean(A1,2), mall = mean(A1(:)) The slash operators solve a system of linear equations. Left-divide (\) solves equations of form Ax=b; right-divide (/) solves xA=b. A=randi(4,3); b=randi(8,3,1); x=A\b c=b.'; x2=c/A For more info, see:

26 Tutorial Outline Intro & MATLAB Desktop Windows (15 min)
MATLAB Language Command Syntax (15 min) Finding Help and Documentation (5 min) Creating Scripts and Functions (5 min) Running Code (5 min) Setting Breakpoints and Debugging (5 min) Additional Topics, as time permits (<10 min) Programming Best Practices Global Variables Matrix Manipulation Questions? This tutorial, “Getting Started with MATLAB”, is intended to take 60 minutes, with a 5-minute period at the end for questions.


Download ppt "Getting Started with MATLAB"

Similar presentations


Ads by Google