MATLAB
Introduction
The information is saved in the matrix – matrix and vector oriented. M-files (command files
saved for later execution with extension .m) are simply text files that can be written and
read using standard text editors.
Command line window– directly communicating with Matlab. If the script is straight forward
it can be written there (>> prompt). If it is a more complicated script, use the editor.
Execute a program MATLAB will look for it in the current folder
Command = communication computer language
General purpose commands:
- clc. Clear screen
- clear. Clear workspace, important to clear memory
- lasterr. Last error message
- lastwarn. Last warning message
- ver. Version
- path. Control MATLAB’ s directory search path
- debugger. Looks for errors in the code
Workspace provides access to the variables built up during a session (also updated
variables). Summary of the variables´ values (if you click you can see the matrix where it is
stored and change the value of the variable)
Matrices (more than one dimension, vector is a subclass of matrix). Vectors and matrices
have to contain the same type of data
- Semicolons and newlines separate rows e.g. B [1 2 3; 4 5 6], B (2,3) = 6
- Spaces and commas separate columns e.g. A [1 2 3]
- Access an element of a vector vector_name(position)
- An element of a matric matrix_name (row, column)
- An entire row of a matrix: matrix_name (row, :). B(1,:) the first row
- An entire column of a matrix: matrix_name(:, column)
- A part of the matrix B (row_ini:row_end , col_ini:col_end). B (1:2 , 2:3) – from one to
second row. Second and third row -> 2,3,5,6
- You can´t exceed the matrix dimensions
- Modify an element of a matrix matrix_name (row, column) = new value
- Assign a value to a position out of the range possible, rest of the matrix filled with zeros
-
- Delete an element of a vector: vector_name (position) = [ ]; A (3) = [ ];
- Delete the row of a matrix: matrix_name (row, :) = [ ];
- Delete the column of a matrix: matrix_name (:, column) = [ ]; B(:,2) = [ ];
- To give initial values to all the elements in a matrix or vector use zeros and ones
- Zeros(number of rows, number of columns). A = zeros(2,2)
- size (matrix) returns the size of the matrix. Size(A) ans = 2 2
Syntax
Syntax necessary to build and evaluate expressions. An expression is a construction composed by
variables, values, operators and function calls. MATLAB evaluates the expressions from left to right.
If the expression has not been explicitly assigned to a variable MATLAB automatically stores the
result in the special variable ans.
Variables
Def. A symbolic name given to an unknown data that permits the name to be used
independently of the information that represents. Container which holds values in the
computer´s memory. Their values are normally changed (assign, modify, operate). They can
be created directly in the command window or in the editor
Identifier/Name
1- Meaningful names
2- Variable = expression (equal represents assignment and not an arithmetic equality.
ALWAYS in the left (guest = 20)
3- Case sensitive, characters together
4- Don´t use keywords
Types – restricts the values that a variable can include and determines the operations you
can execute (e.g. text/number)
1. Boolean. Logical = true (1) or false (0)
2. Integers. Unsigned (starts from zero, Uint8 (0-255)) and signed (includes negative, int8 (-128
– 127 since one of the bits is used to represent the sign)
3. Floating-point. Single (32 bits) or double (64 bits and by default, specify to save memory
otherwise). Numeric variables are automatically stored as 64-bit (8-byte)
4. Char. Put character within single quotation marks (var = ‘T’, str = ‘Hello’). Turn input to string
‘s’
5. Cells. Array of indexed cells, each capable of storing an array of a different dimension and
data type. A = {‘Hello’, 0.23, [0 1 2 3]}
6. Structures: provide the means to store hierarchical data together in a single entity by
associating named fields to different information. s = struct(‘ a ’ , ‘Hello’ , ‘b’ , ‘0.23’ , ‘ c ’, [0
1 2 3]);
Matlab is not strongly-typed. Variables don´t need to be declared prior to use.
Operators
: generates a sequence of values and it´s useful to create matrices and vectors. Initial value :
step : final value
Relational operators. The result is a Boolean value (true or false)
- < Less than
- <= Less than or equal to
- > Greater than
- >= Greater than or equal to
- == Equal to
- ~= Not equal to
Logical operators
- & and
- | or
- ~ not
- xor exclusive or
Short-circuit operators
- && and
- || or
Screen output, input and comments
; it won´t print the variable every time
Disp(‘show this text’). Displays a fixed line of text and does not contain any variable
fprintf(‘text’, var1, var2…) .Writes formatted data to the command window
- Control characters included in the text. %d for variables of integer type, %f for variables
of float point type and %c for variables of char type
- %[Link] (where n is the number of integer digits to display and m is the number of
decimals. If n is not provided MATLAB displays all the integer digits
- \n or \t new line and new tab
- ‘’ single quotation mark
- %% percent character
Comments. Help the reader understand the steps. % Any following text after the % is
ignored by Matlab. % {and}%