You are on page 1of 6

Contents

Contents

Number arrays
Arrays
Arrays in
in
Matlab
Matlab
Character arrays

Writing to files

Dr. Mustafa M. Aziz


Harrison 310
M.M.Aziz@ex.ac.uk

1 2

Arrays • Elements of an array are accessed through


Arrays in
in MATLAB
MATLAB an index:
>> y(2)
ans =
• Arrays are variables that can store many 4.8
values:
>> y(5)
ans =
y = [2.1 4.8 5 3.5 9 7.7]; 9
• Array elements start from index 1.
This declares y as an array with 6 elements.
• The index must be an integer:
Index
Index y(2.5) Error!
Error!
y(1) y(2) y(3) y(4) y(5) y(6)
• The index number cannot be greater than
2.1 4.8 5 3.5 9 7.7 the array size:

y(9) Error!
Error!

3 4
Accessing Array
Elements
• Array elements may be accessed using an
• Can treat array elements as normal index variable:
variables:
z = y(2) + y(4);
>> i = 4;
x = y(5)/7; >> y(i)
ans =
• Arrays and array elements can be used in
mathematical functions: 3.5

>> exp(-y(2))
>> y(i+1)
ans = ans =
0.0082 9
>> sqrt(y)
ans =
1.4491 2.1909 2.2361
1.8708 3.0000 2.7749

5 6

% Using a for loop to print the


Changing Array
% contents of array y.
Elements
y = [2.1 4.8 5 3.5 9 7.7];

start
• Array elements may be changed using
start increment
increment assignment statements:

stop
stop
for i=1:1:6
>> y(2) = 100;
fprintf('y(%g)=%g \n',i,y(i));
>> y(4) = 200;
end
>> y(5) = sin(pi/2);
y(1)=2.1 >> y
y(2)=4.8 ans =
y(3)=5 2.1 100 5 200 1 7.7
y(4)=3.5
y(5)=9
y(6)=7.7

7 8
Problem: Text Strings
(a) Write a program that calculates:
y = 2exp(-3x)
• A text string is an array of characters
enclosed within single quotes:
where x varies from 0 to 10 in steps of 0.1.
(b) Plot y against x in MATLAB. s = 'Line one';

This declares s as a character array with 8


elements.

Index
Index

s(1) s(2) s(3) s(4) s(5) s(6) s(7) s(8)

L i n e o n e

White
Whitespace
spacecharacter
character

9 10

The %s formatting
• Access string elements using index: character
>> s(2)
ans = • To display text strings on the screen with
i fprintf, you need to use the %s
>> s(7) formatting character:
ans =
n str = 'Hello world!';

• Change string elements: fprintf('My text: %s', str);

>> s(3) = 'A';


>> s(6) = 'B'; My text: Hello world!
>> s
s =
LiAe Bne

11 12
% Using a for loop to print the Problem:
% contents of a character array. Create the text string:

str = 'Hello world!' 'I hate Spam'

MATLAB a) Write a program that traverses the text


MATLABfunction
functionreturning
returningthe
thenumber
number
of string and replaces the letter 'a' with capital
of elements in an array; 12 in our case
elements in an array; 12 in our case
'A'.
N = length(str); b) Print the modified string on the screen.
for i=1:1:N
fprintf('str(%g) = %s \n'
,i,str(i));
end

str(1) = H
str(2) = e
. . .
str(12) = !

13 14

The '.' operator The '.' operator

• Matlab provided the '.' operator as a • For example, to perform an element by


element multiplication of two arrays, we
shortcut for performing arithmetic
can write:
operations on arrays.

• This operator can be used with the x = [1 2 3 4];


standard mathematical operators to y = [5 6 7 8];
z = x .* y Element
Element bybyelement
element
operate on arrays.
multiplication
multiplication
z =
5 12 21 32
.* Array multiplication

./ Array division
• For simple tasks, the '.' operator can save
on using loops (which are slower in
Matlab).
.^ Array power
• Note that in using the '.' operator with
arithmetic operators, the two arrays in
question must be the same size.

15 16
Problem: Writing
Writing to
to Files
Files in
in Matlab
Matlab
Write a program that uses for loops to do
an element-by-element multiplication of the • The fprintf function in Matlab can be
two following vectors and displays the used to save text and data to files.
result on the screen:
x = [1 2 3 4 5] y = [6 7 8 9 10] • To create/open a file in Matlab, the fopen
function can be used:

Read,
Read,
File
File Output
Outputfile
file write,
write,
variable
variable name
name append
append

fid = fopen(filename, open_mode)

• For example, to create the file "test.txt" for


writing we can have:
fid = fopen('test.txt', 'w');

17 18

Opening Modes
• Once the file is created/opened using
fopen, then the file identifier can be used to
redirect the output to the file with fprintf.
Open • For example, to store the text "Hello World"
mode Action
into the file "test.txt" (with file identifier fid)
then we can type:
'r' Open file for reading.
fprintf(fid,'Hello World');
'r+' Open file for reading and writing.
• The same techniques for printing variable
'w' Create file for writing. values with fprintf can also be used. For
example:
'w+' Create file for reading and writing.
x = 5;
'a' Open file for appending.
y = x*7;
'a+' Open file for reading and appending.
fprintf(fid,'x=%g and y=%g',x,y);

19 20
fclose Function

• After finishing using the file, the file must


be closed using the function fclose along
with the file variable:
fclose(fid);

Problem:
Write a program for calculating y = tan-1(x)
for values of x between -5 and 5 in steps of
0.1 and storing the values of x and y as two
columns in a file.
Open the file in Microsoft Excel and plot the
data.

21

You might also like