0% found this document useful (0 votes)
47 views

1: Variables in Matlab: Syntax

The document describes various MATLAB functions and syntax including variables, data types, expressions, loops, conditional statements, functions, structures, classes, matrices, arrays, image processing functions, and operators. Examples are provided to demonstrate the use of each.

Uploaded by

jazib
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

1: Variables in Matlab: Syntax

The document describes various MATLAB functions and syntax including variables, data types, expressions, loops, conditional statements, functions, structures, classes, matrices, arrays, image processing functions, and operators. Examples are provided to demonstrate the use of each.

Uploaded by

jazib
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1: Variables in Matlab

Syntax:
variables comp_var1 = { value , 'unit' }; end
variables comp_var2 = { value = { value , 'unit' },priority = priority.value }; end
variables domain_across_var1 = { value , 'unit' }; end
variables(Balancing = true) domain_through_var1 = { value , 'unit' }; end
Example:
This example initializes the variable w (angular velocity) as 0 rad/s:
Variables w = { 0, 'rad/s' }; % Angular velocity
end
This example initializes the variable x (spring deformation) as 0 mm, with
high priority:
variables
x = { value = { 0 , 'mm' }, priority = priority.high }; % Spring deformation
end
2: Data Types in Matlab
Data Type Description

int8 8-bit signed integer


double double precision numerical data
char character data strings are stored as vector of character
user classes objects constructed from a user-defined class
Syntax:
S=double(x)
S = char(A)
S = char(A1,...,AN)
S = struct(field,value)
A = float('double')

Example:
str = 'Hello World!'
n = 2345
d = double(n)
un = uint32(789.50)
rn = 5678.92347
3: Expressions in Matlab
Syntax:
Suppose you want to use a symbolic variable to represent the golden ratio
Φ=1+√5/2
The command
phi = (1 + sqrt(sym(5)))/2;

Example:
f = phi^2 - phi - 1
returns
f=
(5^(1/2)/2 + 1/2)^2 - 5^(1/2)/2 - 3/2
Now suppose you want to study the quadratic function f = ax2 + bx + c. First,
create the symbolic variables a, b, c, and x:
syms a b c x
Then, assign the expression to f:
f = a*x^2 + b*x + c;

4: For loop

Syntax:
for index = values
statements
end

Example:
Create a Hilbert matrix of order 10.
s = 10;
H = zeros(s);
for c = 1:s
for r = 1:s
H(r,c) = 1/(r+c-1);
end
end
5: While loop

Syntax:
while expression
statements
end

Example:
Use a while loop to calculate factorial(10).
n = 10;
f = n;
while n > 1
n = n-1;
f = f*n;
end
disp(['n! = ' num2str(f)])
n! = 3628800

6: Switch Statement

Syntax:
switch switch_expression
case case_expression
statements
case case_expression
statements
...
otherwise
statements
end

Example:
Display different text conditionally, depending on a value entered at the command
prompt.
n = input('Enter a number: ');
switch n
case -1
disp('negative one')
case 0
disp('zero')
case 1
disp('positive one')
otherwise
disp('other value')
end
At the command prompt, enter the number 1.
positive one
Repeat the code and enter the number 3.
other value
7: if statement

Syntax:
if expression
statements
else
statements
end

Example:
Test if any results are true using the any function.
limit = 0.75;
A = rand(10,1)
A = 10×1
0.8147
0.9058
0.1270
0.9134
0.6324
0.0975
0.2785
0.5469
0.9575
0.9649
if any(A > limit)
disp('There is at least one value above the limit.')
else
disp('All values are below the limit.')
end
8: if-else-statement

Syntax:
if expression
statements
elseif expression
statements
else
statements
end
Example:
Create a matrix of 1s.
nrows = 4;
ncols = 6;
A = ones(nrows,ncols);
for c = 1:ncols
for r = 1:nrows
if r == c
A(r,c) = 2;
elseif abs(r-c) == 1
A(r,c) = -1;
else
A(r,c) = 0;
end
end
end
9. Functions
Syntax:
function f = fact(n)
f = prod(1:n);
end
Example:
x = 3;
y = 2;
z = perm(x,y)
function p = perm(n,r)
p = fact(n)*fact(n-r);
end
function f = fact(n)
f = prod(1:n);
end
Call the script from the command line.
mystats
z=6
10. Structures
Syntax:
s = struct
s = struct(field,value)
s = struct(field1,value1,...,fieldN,valueN)
s = struct([])
s = struct(obj)
Example:
field = 'f';
value = {'some text';
[10, 20, 30];
magic(5)};
s = struct(field,value)
11.Classes
Syntax:
ClassName = class(object)
obj = class(s,'class_name')
obj = class(s,'class_name',parent1,parent2,...)
obj = class(struct([]),'class_name',parent1,parent2,...)
obj_struct = class(struct_array,'class_name',parent_array)
Example:
Return the class of Java® object obj:
import java.lang.*;
obj = String('mystring');
class(obj)
ans =
java.lang.String
Return class of any MATLAB variable:
h = @sin;
class(h)
ans =
function_handle

12: Matrix
Syntax:
matrix(Array)
matrix(List)
matrix(ListOfRows)
matrix(Matrix)
matrix(m, n)
matrix(m, n, Array)
Example:
A := matrix([[1, 5], [2, 3]])

13: Array
Syntax:
array(m1 .. n1, <m2 .. n2, …>)
array(m1 .. n1, <m2 .. n2, …>, index1 = entry1, index2 = entry2, …)
array(m1 .. n1, <m2 .. n2, …>, List)
array(<m1 .. n1, m2 .. n2, …>, ListOfLists)
Example:
array([[1,2],[3,4],[5,6]]) = array(1..3, 1..2, [[1,2],[3,4],[5,6]]);

14: Colon Operator , :


Syntax:
Starting value: ending value
j:k
Examples:
Using the colon with integers,
D = 1:4
results in D= 1 2 3 4
15 : Operators , + , - , /

Plus,+

Syntax
C=A+B
C = plus(A,B)

Example:
A = [0 1; 1 0];
C=A+2
C= 2 3
3 2
Minus, -

Syntax
C=A-B
C = minus(A,B)
Example:
Create an array, A, and subtract a scalar value from it.

A = [2 1; 3 5];
C=A-2
C = 0 -1
1 3
Prod,*

Syntax
B = prod(A)
B = prod(A,dim)
B = prod(___,type)
Example:

A=[1:3:7;2:3:8;3:3:9]
B = prod(A)
B = 6 120 504
16: imread

Syntax
A = imread(filename)
A = imread(filename,fmt)
A = imread(___,idx)
A = imread(___,Name,Value)
[A,map] = imread(___)
[A,map,transparency] = imread(___)
Example:
Read a sample image.
A = imread('ngc6543a.jpg');
image(A)

17: imwrite

Syntax:
imwrite(A,filename)
imwrite(A,map,filename)
imwrite(___,fmt)
imwrite(___,Name,Value)
Example:
A = rand(50);
imwrite(A,'myGray.png')

18: imfinfo

Syntax:
info = imfinfo(filename)
info = imfinfo(filename,fmt)
info = imfinfo(URL)

Example:
Find information about the example image, ngc6543a.jpg.
info = imfinfo('ngc6543a.jpg')
info =

Filename: 'matlabroot\toolbox\matlab\demos\ngc6543a.jpg'
FileModDate: '01-Oct-1996 16:19:44'
FileSize: 27387
Format: 'jpg'
FormatVersion: ''
Width: 600
Height: 650
Comment: {'CREATOR: XV Version 3.00b Rev: 6/15/94 Quality
= 75, Smoothing = 0
'}

You might also like