You are on page 1of 25

CPEG201L

WHAT IS MATLAB?
MATLAB

 Commercially available
 Mathematical computational tool
 Stands for Matrix Laboratory
Why MATLAB?

 Can be easier to use for some applications


 Versatile with many toolboxes and a lot of support
 Not a general-purpose programming language.
Releases

 MATLAB gets two versions a year: an (a) version and a (b) version
 MATLAB is updated regularly, and some functions are changed slightly
 AUK students have free MATLAB access as long as they sign up for a
MathWorks account using their AUK emails
Interpreter Language

 MATLAB is an interpreter language (similar to Python)


 This means that code runs line by line
 This is different than Java where the entire code is compiled and THEN it
runs on the machine
 This allows for us to use the MATLAB command prompt to try out, test and
play with the code line-by-line
 However, this can make MATLAB somewhat slower than C or Java for
many large-scale algorithms
When do we use MATLAB?

 MATLAB is mostly a tool by engineers for engineers


 Multiple toolboxes in fields of electrical, mechanical, biomedical, chemical
engineering and many other fields
 It is a natural language for multi-variate problems where everything is a
matrix
MATLAB Interface

 MATLAB uses multiple windows to display information about currently open


scripts and files
 It also shows you latest used commands and has options where you can
see the current variables available
Workspace Window
Current Folder
Interface

Window
Example

Lists files stored in the Command Window


current directory
Enter commands at the prompt

MATLAB Windows
Command
History Window
Records all commands
issued in the command
window – including
mistakes
IN CLASS DEMO

MATLAB interface.
Variables

 Unlike Java or C, you do not need to declare the variable type when
creating a variable in MATLAB
 Furthermore, MATLAB does not force you to end lines of code with a semi-
colon
 A=3
 Should be read as A is assigned a value of 3
 Use the variables in subsequent calculations
Naming Variables

 All names must start with a letter


 They may contain letters, numbers and the underscore ( _ )
 Names are case sensitive
 There are certain keywords you can’t use
Use the iskeyword function for a list of
keywords
iskeyword

ans =
'break' 'global'
'case' 'if'
'catch' 'otherwise'
'classdef' 'parfor'
'continue' 'persistent'
'else' 'return‘
'elseif' ‘spmd’
'end‘ 'switch'
'for‘ 'try'
'function' 'while'

Keywords are not acceptable variable names


Functions

 In Java, you called them “Methods”. But in MATLAB, they are called
functions
 MATLAB functions are subroutines (that can be defined by the user or built
into MATLAB/MATLAB toolboxes)
 We will learn many functions throughout this lab, and we will learn how to
write our own
 Generally speaking, you should never name variables the same name as a
function!
 This might “run”, but it is a bad habit and can cause logical errors
Which of these names are
allowed in MATLAB?

 test
 Test
 if
x
 my-book
x
 my_book
 Thisisoneverylongnamebutisitstillallowed?x
 1stgroup
x
 group_one
 zzaAbc
 z34wAwy?12#
 sin x x
 log bad
idea
Order of Operations

 Just like mathematics: P.E.D.M.A.S.


 Use only ( )
 { } and [ ] mean something different
 MATLAB does not assume operators

5 * (3+4) not 5(3+4)


Matrices in MATLAB
The basic data type

 Group of numbers arranged into rows and columns


 Single Value (Scalar)
 Matrix with one row and one column
 Vector (One dimensional matrix)
 One row or one column
 Matrix (Two dimensional)
Scalar Calculations

 You can use MATLAB like you’d use a calculator

Command
Prompt
>> 9 + 10
ans=19
Result
IN CLASS DEMO

Creating variables, scalars, matrices, simple arithmetic.


Saving Your Work

 If you save a MATLAB session performed in the


command window, all that is saved are the
values of the variables you have named
Variables are saved,
not the commands in
the command window
Save either by using the file menu or...

Save with a command in the


command window
Script M-files

 If you want to save your work,


(the commands you entered)
you need to create an M-file (Script file).
 File->New->M-file
 Type your commands in the edit window that opens
Script M-files

 The file can be saved into the current folder/directory


 It runs in the command window
Comments

 The % sign identifies comments. You need one on each


line.
 Be sure to comment your code
 Add your name
 Date
 Section #
 Assignment #
 Descriptions of what you are doing
Some Commands

 Help, save, clc, and clear.

 sum(z) finds the sum of the elements of the vector z.

You might also like