Professional Documents
Culture Documents
1st - Yr - Lecture 11 - NEW
1st - Yr - Lecture 11 - NEW
• String manipulation
• Text file opening and closing
• The commands sprintf and fprintf
• The commands fgetl and fgets
2
Last Lecture
• In the last two lectures we considered function files.
3
TODAY’S LECTURE
• Today you will be learning several MATLAB commands -
a summary of the commands will be provided at the end
of the lecture for you to review.
4
An Engineering Example…
• Flow past an aircraft is an active research area.
We use MATLAB. 5
String Manipulation
Creating a string in MATLAB
• Creating a string in MATLAB is easy! Enclose
the string in single quotes:
6
String Manipulation
• A string is essentially an array of letters:
Variable1 = 'This is a string';
>> Variable1(1)
ans = As with any other array, you
T can refer to individual elements
>> Variable1(2) within a string variable.
ans =
h You can also manipulate
>> Variable1(3) individual elements of a string:
ans =
i
>> Variable1(4)
ans =
>> Variable1(5) = 'A'
s
>> Variable1(11:16) Variable1 =
ans =
string
ThisAis a string
>> >>
7
String Manipulation
Creating a string from other variables
• You may want to change a number (either a double
or an integer) into a string, you can then use this in
creating other strings.
String = num2str(MATRIX)
String = sprintf(format,A,…)
• The format defines what the new string variable will look
like.
11
The sprintf Command
Some Simple Examples
12
The sprintf Command
Formatting strings using sprintf
• Now we will use sprintf to add two strings together:
Variable2 = num2str(pi,5);
Final_string =
I am interested
• Notice, that I haveinused
the the
number 3.1416 in the format to
%s command
inform sprintf that I will be entering in a string. There are two
strings entered here, Variable1 and Variable2.
13
The sprintf Command
Formatting strings using sprintf
• We can also use sprintf to include numbers in a
string:
Final_string =
I• am interested
Notice, that I havein thethenumber
used 3.141593
%f command in the format to
inform sprintf that I will be entering in a floating point number.
The floating point number in this case is the number . Notice
that MATLAB prints this number to 8 decimal places.
14
The sprintf Command
Formatting strings using sprintf
• In fact it is possible to specify the following
types of numbers to include in a string:
ans = ans =
3 1.772454e+000
15
The sprintf Command
Formatting strings using sprintf
• Finally, you can also specify the number of
decimal places, and also the number of
string elements that a number will take up:
• The first thing we have to do is tell MATLAB to open a file to either write to
or read from.
• To do this we use the command fopen:
FID = fopen(filename,permission)
• FID is a variable MATLAB uses to identify the file we are using(fid is a
number assigned to a file when it is opened, and it is used in all reading,
writing and control operations on that file).
18
Opening and Closing Text Files
Directory = 'c:\matlab_example';
Filename = 'text_file_example.txt';
file_id = fopen(fullfile(Directory,Filename),'r');
• In this case, MATLAB sets file_id = -1. The value -1
disp(file_id);
indicates that the file does not exist.
20
The fprintf Command
Writing to a text file
• Now that we have opened a text file, we can
write strings to the text file.
fprintf(file_ID,FORMAT,A,...)
21
The fprintf Command
An example
• The following code writes data to a text-file:
%%% Open a text file to write information to.
directory = ‘z:\';
filename = 'example_file.txt';
fid = fopen(fullfile(directory,filename),'w');
n pi^n
--------------------
1 3.1416
2 9.8696
3 31.0063
4 97.4091
5 306.0197
6 961.3892
7 3020.2932
8 9488.5310
9 29809.0993
23
The fprintf Command
Writing to a the command window
• We can also use fprintf instead of the disp
command.
24
Reading data from a text file
Reading a line of code from a file
• There are two ways to read information
from a textfile. You can then store this
information into variables in MATLAB.
25
Reading data from a text file
• Using the fgetl command, we can read the next line from a
text file. Recall the textfile we created in the previous
example.
directory = ‘z:\';
filename = 'example_file.txt';
fid = fopen(fullfile(directory,filename),'r');
text_string = fgetl(fid);
text_string = fgetl(fid);
text_string = fgetl(fid);
text_string
text_string =
n pi^n
>> 26
Reading data from a text file
Reading a line of code from a file
• This means that we can get MATLAB to read a file into a
series of strings. We could use this to read in data for a
program to work.
directory = ‘z:\';
filename = 'example_file.txt'; We have now
fid = fopen(fullfile(directory,filename),'r'); read in all the
text from the
for jj = 1:4
buffer = fgetl(fid); example text file
end and converted all
of the numbers
for jj = 1:10
string = fgetl(fid); to a matrix called
Numb(:,jj) = str2num(string); Numb.
end 27
Reading data from a text file
The fscanf command
• There is a second way to read information from a
text file. Read formatted data from text file.
VARIABLE = fscanf(FID,FORMAT,A,…)
0.00 1.00000000
0.10 1.10517092
...
1.00 2.71828183
28
Reading data from a text file
The fscanf command
29
Fscanf
Read formatted data from file
Syntax
A = fscanf(fid, format)
[A,count] = fscanf(fid, format, size)
Description
A = fscanf(fid, format)
reads data from the file specified by fid, converts it according to
the specified format string, and returns it in matrix A.
Argument fid is an integer file identifier obtained from fopen.
format is a string specifying the format of the data to be read.
[A,count] = fscanf(fid, format, size)
Reads the amount of data specified by size, converts it according
to the specified format string, and returns it along with
a count of values successfully read.
size
isan argument that determines how much data is read. Valid options are
n
Read at most n numbers, characters, or strings.
Inf
Read to the end of the file.
[m,n]
Read at most (m*n) numbers, characters, or strings. Fill a matrix of at most m
30
Opening and Closing Text Files
fclose(file_id);
• To close all the open text files, you can type:
fclose('all');
31
Commands you have learnt
To open and close a text file
FID = fopen(FILENAME,PERMISSION)
opens the file FILENAME for read/write access.
fclose(fid)
closes the file FILENAME
string = num2str(number)
converts the values in the variable number, to a string
stored in the variable string. 32
Commands you have learnt
To write information to a file
fprintf(FID,FORMAT,A,...)
writes information to a text file using the format
provided by the program. The variables A,… are
used by the format to determine the output to the
text file.
VAR = fscanf(FID,FORMAT,A,...)
reads information from a text file using the
format provided by the program. The
variables A,… are used by the format to
determine the input to the text file.
34
Conclusion
• In today's lecture we have
considered how to:
– Create text strings
– Read information from a file
– Write information to a file
35