- General
- Common Commands
- Arrays
- Plots
- Operators
- Flow control
- Logical indexing
- Images
- Functions
- String functions
- Structs
- Cells
- File I/O
- A
;
can be used to separate commands without echoing the result. - A
,
can be used to separate commands with echoing the result.
x = 3; y = 4, z = 5; %This will echo y=4
- Puting
...
will allow the command to continue on the next line. - Convention: Vectors start with a lower-case letter while matrices start with a capital letter.
- Indices start with 1
inf
is a keyword to represent infinity.
>> clear
Clears the variables list.
>> clc
Clear command window.
>> help <cmd>
Prints the help page of a command
>> doc <cmd>
Shows the the documentation page of a command
>> format <param>
Changes the way the output is printed (<format comapct> reduces line spacing)
>> pause(x)
Pauses execution for x seconds
>> fprintf('%d items at %.2f each. Total=%5.2f\n', n, price, total)
Prints a formatted string (.2f: float with 2 decimal places, 5.2f: float with 5 digits totally)
>>
>> x = input('Enter a number:')
Accept user input
>> whos [<vars>]
Prints details about the variables or prints all workspace variable.
>> save [file_name] [<vars to save>]
Save all or some of workspace variables to matlab.mat file (default) or to the specified file name.
>> load [file_name] [<vars to restore>]
Restore a saved workspace. Looks for matlab.mat if no file name is specified.
Definition: X = [1,2,3] OR X = [1 2 3]
Length: length(X) %will return 3
Definition: Y = [1 2 3; 3 2 1]
Size: size(Y) % Returns 2 3
size(X) % Returns 1 3
Syntax: X = start:step:finish
X = start:finish
Example: X = 1:3:11 % X = 1 4 7 10
X = 5:-2:1 % X = 5 3 1
X = 2:5 % X = 2 3 4 5
>> X(2,2) % Returns the element at position 2,2
>> X(2, [1 3]) % Returns the 1st and 3rd element of the 2nd row e.g. ans = 4 6
ones(x)
,ones(n,m)
,zeroes(x)
,zeroes(n,m)
create anx*x
orn*m
matrix of ones/zeroesdiag(X)
creates matrix withX
as its diagonal and the rest elements are zeroes.
- end is a keyword that refers to the last index e.g. in
X(2,end)
it refers to the last column. :
can be used instead of 1:end e.g.X(1,:)
returns the first row.X(:)
will return all elements ofX
as a vector.'
is used to get the transposition of a matrix. e.g.if size(X)=1 2 then size(X')=2 1
.* ./ .\ .^
are used for array operations (each element with its counterpart).- Matrix multiplication requires compatible dimensions i.e.
Z = X*Y => LxN = LxM * MxN
- When using two variables to get the output of max function we get the max and its index.
- When indexing the matrix using one index, it is treated as a large vector.
- A code section can be created by putting
%%
in its beginning. We can then just run this section be choosing theRun section
command from the toolbar. - The Publish command will create an html page with sections and their code.
- To get a certain function output argument use tilde as a place holder:
[~, ~, raw] = xlsread('my_file.xls')
>> plot(X,Y) % X,Y can be variables or vectors
>> plot(X,Y,lineSpec) % lineSpec changes how the plot is drawn ('r*' will use red stars)
>> bar(X,Y) % Draw a bar graph
>> pie(X) % Draw a pie chart
>> xlabel/ylabel(string) % Change axis label
>> title(string) % Change plot title
>> axis([x1,x2,y1,y2]) % Override automatic axis assignment
>> legend(str1,str2,..) % Insert legends for the drawn plots
>> grid <on/off> % Turn on/off the grid when drawing plots
>> axis <on/off>
>> hold <on/off> % Don't erase the previous plot when redrawing
>> figure(n) % Create a new figure numbered n or set n as the active figure
>> close(n) % Close figure n
>> close all % Close all figures
^(exp) mod(x1,x2)
and or not xor any all ~ & |
~= == && ||
if condition1
% statements
elseif condition2
% statements
else
% statements
end
for i = list | for i=1:N
...
break/continue
end
while <condition>
...
end
Used to create an array A
using the elements of an array B
that matches 1's in a logical array C
.
B = 1 -1 3
C = B>0 => C = 1 0 1
A = B(C) => A = 1 3
A = B(B>0) => A = 1 3
% Do operations on elements of B
B(B<0) = 0 => B = 1 0 3
B(B<0) = B(B<0) + 10 => B = 1 9 3
The result of logical indexing is ALWAYS a vector (when used on the right side of the equation)
>> pic = imread('pic.jpg') % Stores the image as a matrix
>> image(pic) % Display the actual image stored in the matrix
function [<return_vars vector> =] <name>[(pram1,param2,..)]
<statements>
end
function [A,s] = func(x1,x2)
A = [x1:x2];
s = sum(A);
end
% In command window:
>> [a b] = func(1,3) % a= 1 2 3 b= 6
There can be many functions in a .m
file but only the first one can be called from the outside.
global
can be used to declare a variable to share it by functions and the workspace.persistent
is used to declare a variable that keeps its value between function calls (static).nargin
andnargout
store the number of input/output arguments of a function which is useful for polymorphism.
rand(n,m) returns n*m random matrix with values in the range [0,1]
randi(x/[x1,x2],n/<n,m>) returns n*m (or n*n) random integer matrix with values from 1 to x or from x1 to x2.
randn(n/<n,m>) random matrix with normal distribution (mean=0, standard diviation=1).
fix(X) return the integer part of numbers
isscalar(var) checks if a variable is scalar.
error(string) Prints a text in red and quits the function.
isempty(var) Checks if a a declared variable has been defined.
logical(X) Converts X into a logical array ( return X(i)>0?1:0
Type conversion int8(x), uint32(x), double(x)
class(var) Returns the type of the variable
intmax/intmin('int32'), realmax/realmin('double') Types range.
[num, txt, raw] = xlsread(file_name [, sheet][, cells]) Reads an Excel file and stores it in 3 arrays for numbers, strings and everything. E.g. >> [~,~,all] = xlsread('file.xls', 2, 'D2:E6')
- char(var): Convert to string.
- strcmp/strcmpi(s1, s2): Compare strings. Case sensetive/insensetive.
- strncmp/strncmpi(): Compares the first n letters.
- findstr(s1, s2): Searches for s2 inside s1
- strmatch(): Searches an array for rows starting with string.
- isletter(): Finds letters
- isspace(): Finds white spaces
- upper/lower(str): Convert to upper/lower case
- str2num(), num2str(): Convert between numbers and strings.
- str=sprintf(): Similar to fprintf but prints to a string
- strrep(str, s1, s2) Looks for s1 in str and replaces it with s2
- struct1.field1 = 5 % Matlab creates a struct called struct1 with a field called field1.
- struct1 = struct(field1_name, field1_val, field2_name, field2_val, ...)
struct1 = struct('Name', 'John', 'Age', 25)
- isstruct(var): Checks if var is a struct.
- isfield(struct1, 'age'): Checks if there is a field called 'age' in struct1.
- struct2=rmfield(struct1, 'age'): Creates struct2 that equals to struct1 after removing field 'age'.
- setfield(), getfield(): Sets/returns the value of a field in a struct.
- orderfields(): Changes fields order.
- They are pointers to other variables.
- Mostly used in cell arrays.
- Cells in cell arrays are accessed using big brackets, e.g.
- p{1} = 3.1 % create a cell array named p with the first cell pointing at 3.14
- cell(3,3) % Create a 3x3 cell array
- p = {3.14, 'hi'; 'bye', 5} % Create and populate a 2x2 cell array
- celldisp(): Show all objects pointed at by a cell array
- cellfun(): Apply a function to all the objects pointed at by a cell array
- cellplot(): Plot cell array elements
- cell2struct(): Convert cell array into a struct array
- num2cell(): Convert a numeric array into a cell array.
- deal(): Copy a value into output arguments
- cellfun(func, c_arr): apply function func to c_arr elements
- b = a(cellfun(@(x)isempty(strfind(x,str)),a)); % return elements that do not contain str.
- out = cellfun(@(x) x(1:3), days, 'UniformOutput', false) % return first three characters of each element. The output can be ununiform.
- Delete an element
- p(2,2) = [] % remove one element
- p(2,:) = [] % remove the second row
- Linux file and directory management commands can be used in matalab in a similar way, but options are not supported.
- cd C:// <=> cd('C://') <=> x='C://'; cd(x)
- pwd, ls
- str = fileread(filename): Reads a file to a string
- fid = fopen(filename, <permission>): Open file and return fileID (negative if unsuccessful).
- fclose(fid)
- Permissions: rt-open/read, 'wt'-open/create/overwrite/write, 'at'-open/create/write/append, 'r+t'-open/read/write, 'w+t'-open/create/overwrite/read/write, 'a+t'-open/create/append/read/write
- fprintf(fid, string, format_args)
- str = fgets(fid): Reads one line from a file. Returns -1 when it reaches EOF.
- C = textscan(fileID,formatSpec): Read formatted data from text file or string. E.g. Create a float array:
>> str = '0.41 8.24 3.57 6.24 9.27';
>> C = textscan(str,'%f');
- Print a file one line at a time:
while ischar(line) line=fgets(fid);
- File is opend using fopen with omitting the 't' from the permission.
- fwrite(fid, Arr, <data_type>): Writes and array of a certain data type to a file. E.g. >> fwrite(fid, A, 'double')
- Arr = fread(fid, num, <data_type>): Reads num items from fid and store it in Arr. E.g. >> A = fread(fid, inf, 'double')
- The array items are stored in the binary file as a long vector without and dimention information. This information can be stored along side the data to ease retriving it later. This is an example of formating the binary file for storing variable sized arrays:
% write an array to the file:
dims = size(A);
fwrite(A, length(dims), 'double')
fwrite(A, dims, 'double')
fwrite(fid, A, 'double')
% reading from the file
n = fread(fid, 1, 'double')
dims = fread(fid, n, 'double')
B = fread(fid, inf, 'double')
B = reshape(B, dims') % Convert B from vector to an array isequal(A, B) = 1