Professional Documents
Culture Documents
For Engineers
Made by:
Eng. George Iskander Your Feedback is
valuable for me
george.iskander84@gmail.com
Mob: +20 122 750 9507
Output is a
logical type
• A struct data type is a heterogeneous. It can hold different data type in one variable
Struct Array
Heterogeneous (Different Homogenous (Same Data
Data type) Type)
Contain Fields Contain Elements
Reach fields by names Reach elements by indices
– Use a dot .
– Use struct()
Struct Name
Very dynamic
Can make field
names as a
variable
• If you have an array of structure and you want to change the value a certain
field .
>> IDs = {1000, 1001, 1002, 1003};
>> [employee.ID]=IDs{:};
George Iskander - ITI 16
Cell Data Type
• A cell array is used to collect data of different data types.
• Used more than struct data type for collecting different data type
• A cell array can contain cell arrays inside it.
• The cell is a special type of pointers that is known in C/C++.
• Each variable (scalar, vector, array, etc.) is stored in certain place in the
memory with a unique address not continuous.
• A cell is a variable that stores these addresses.
• Mostly used for collecting string sentences.
Try to plot
similar to it
George Iskander - ITI 24
2D – Plot
• legend(‘string1’,string2’,….)
• xlabel(‘A Label for the x-axis’)
• ylabel(‘A Label for the y-axis’)
• title(‘A Title for the plot’)
• axis([xmin xmax ymin ymax])
• text(x, y, ’Your Text’)
• gtext(‘Write a string in a selected place’)
• ginput() To get a value from the graph
to the workspace
George Iskander - ITI 25
2D – Plot
• Example 1: 1
sin(x)
0.8 sin(x-pi/2)
>> x=0:pi/10:2*pi; sin(x-pi)
0.6
>> y1=sin(x);
0.4
>> y2=sin(x-pi/2);
0.2
>> y3=sin(x-pi);
0
>> plot(x,y1,'--',x,y2,':',x,y3)
-0.2
>> legend('sin(x)','sin(x-pi/2)','sin(x-pi)')
-0.4
Try to draw same graph using hold on -0.6
-0.8
-1
0 1 2 3 4 5 6 7
>> x = t+2
>> y = t-2 -5
5
>> plot3(x,y,t) 0
10
5
-5
0
-10 -5
X, Y, Z must be matrices
HINT:
y = a x + b for a first order curve can be written as
𝑎
𝑦 = 𝑥 1 ×
𝑏
we need to find the vector of the coefficients
George Iskander - ITI 39
Other Plotting Functions
This statement preallocates a 100-by-100 matrix of int8, first by creating a full matrix
of double values, and then by converts each element to int8.
Creating the array as int8 values saves time and memory. For example:
>> A = zeros(100, 'int8');
• Multiple function handles can be stored in a cell array (or structure) as if they are
any other variable.
• To check a variable is a function handle, use >>isa(h,'function_handle')
George Iskander - ITI 45
Anonymous Function
What Are Anonymous Functions?
• An anonymous function is a function that is not stored in a program file, but is associated with a
variable whose data type is function_handle.
• Anonymous functions can accept inputs and return outputs, just as standard functions do.
However, they can contain only a single executable statement.
• The @ operator creates the handle, and the parentheses () immediately after the @ operator
include the variables followed by the equation or single statement
Examples
>> sqr = @(x) x.^2;
>> a = sqr(5)
a = 25
>> q = integral(sqr,0,1); %It can be an input to other MATLAB functions
OR q = integral(@(x) x.^2,0,1);
George Iskander - ITI 46
Anonymous Function
Multiple Anonymous Functions
• The expression in an anonymous function can include another anonymous function.
>> g = @(c) (integral(@(x) (x.^2 + c*x + 1),0,1));
• Step by Step
– @(x) (x.^2 + 2*x + 1)
– integral(@(x) (x.^2 + c*x + 1),0,1)
– g = @(c) (integral(@(x) (x.^2 + c*x + 1),0,1));
>> g(2)
ans = 2.3333
Functions with No Inputs
• If your function does not require any inputs. You must use empty parentheses . WHY??
>> t = @() datestr(now); d = t() Check the difference with >> d = t;
d = '18-Dec-2018 05:11:11'
George Iskander - ITI 47
Anonymous Function
• To copy the function handler to another handler omit the parentheses
>> d = t
d = @() datestr(now)
Functions with Multiple Inputs
• myfunction = @(x,y) (x^2 + y^2 + x*y); x = 1; y = 10; z = myfunction(x,y)
z = 111
Functions with Multiple Outputs
You do not explicitly define output arguments when you create an anonymous function. If
the expression in the function returns multiple outputs, then you can request them when
you call the function.
>> mul= @(x,y) size(x*y) ; [row, col] = mul([2;1],[1,2]) Check the result
George Iskander - ITI 48
Anonymous Function
Arrays of Anonymous Functions
>> f = {@(x) (x.^2); @(y) (y + 10); @(x,y) (x.^2 + y + 10)};
>> x = 1;y = 10;
>> f{1}(x), f{2}(y), f{3}(x,y)
Activity : Modify fzeroX() to accept a function handle as the first input and
the second input be the values of x (range of x).