Professional Documents
Culture Documents
Arrays in Matlab Arrays in Matlab PDF
Arrays in Matlab Arrays in Matlab PDF
Contents
Number arrays
Arrays
Arrays in
in
Matlab
Matlab
Character arrays
Writing to files
1 2
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
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';
Index
Index
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!';
11 12
% Using a for loop to print the Problem:
% contents of a character array. Create the text string:
str(1) = H
str(2) = e
. . .
str(12) = !
13 14
./ 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
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
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