File Input/Output


File Input/Output

matlab



Simple file I/O

In Chapter 1, we introduced a number of simple functions for reading from/writing to files:

dlmwrite: write delimiter-separated data to a text file

f = [1 2 3 4 9 8 7 6 5];
dlmwrite('test.txt’,f,’\t’);

load, save: read/write parts of Matlab workspace in ‘native’ MAT format

d = load('test.txt');
e = d / 2;
save('newdata.mat', 'd', 'e'); save('alldata.mat');
clear load('alldata.mat');

Types of external file

  • Text files - consist of sequence of characters in e.g. ASCII format.
  • Binary files - data stored in binary format (i.e. 0s and 1s), not interpreted using standard coing system.
  • MAT files - ‘native’ MatLab binary format.

NB: actually all computer files are binary files, but it is common to refer those that use a standard character coding system as text files

Simple functions for text file input and output

Here are the most common functions used for text file input and output:

  • csvwrite: write data to comma-separated-value file
  • csvread: read data from comma-separated-value file
  • dlmwrite: write data to delimiter-separated file
  • dlmread: read data to delimiter-separated file

For example if the file scanphyslog.txt contains the following respiratory bellows data:

-33 -53 25 -6  0 -1815 0 0 0 0000
-35 -56 24 -9  0 -1815 0 0 0 0000
-37 -59 22 -12 0 -1815 0 0 0 0000
-39 -61 20 -15 0 -1840 0 0 0 0000
-40 -61 18 -18 0 -1840 0 0 0 0000
-40 -60 17 -20 0 -1840 0 0 0 0000
-39 -58 15 -21 0 -1840 0 0 0 0000
-36 -55 15 -21 0 -1840 0 0 0 0000
-32 -50 15 -20 0 -1871 0 0 0 0000
-26 -44 17 -17 0 -1871 0 0 0 0000
-20 -37 19 -13 0 -1871 0 0 0 0000
-11 -30 23 -8  0 -1871 0 0 0 0000
-1  -22 29 -2  0 -1871 0 0 0 0000

It is possible to read in the data and extract the bellows data:

data = dlmread('scanphyslog.txt', ' ');
bellows = data(:,6)

The second argument, ’ ‘, specifies the delimiter. It returns a 13x10 array know as data. The second line then extracts the 6th column (i.e. the bellows data)

However csvwrite, csvread, dlmwrite, dlmread all have limitations as they only work with text files and the data being read/written must all be numeric.

Opening and closing files

Most of Matlab’s more flexible file input/output functions require files to be opened before use, and closed afterwards, e.g.

% open file
fid = fopen('test.m','r');
...
% close file
fclose(fid);

fopen returns a fiel identifier that must be used when referring to the file. The second argument, ’r’, indicates file access permission:

  • ’r’: read only
  • ’w’: write only
  • ’a’: append

A more complex file open/close example

The following example is one of a program that finds the length of a program. In the script the file ’count_lines.m’ is open then closed at the end. The script loops until the end of the file; at each line of data it determines whether it is empty/comment, if it is not it adds one to the count. Right at the end of the program the result is displayed.

% open file
fid = fopen(‘count_lines.m','r'); % initialise count
count = 0;
% loop until end of file
while ~feof(fid)
   % read line from file
   tline = fgetl(fid);
   % is it blank or a comment?
   if isempty(tline) || strncmp(tline,'%',1)
      continue;
   end
   % count non-blank, non-comment lines
   count = count + 1;
end
% print result
fprintf('%d lines\n',count); % close file
fclose(fid);

File I/O functions

Here are a few useful functions involved with opening and closing a file:

  • fopen, close - open/close a file
  • foef - check for end-of-file condition
  • fgetl - read a line from a text file
  • fprintf - write text (to a file…)

Further file input functions

If an operator wishes to read the following systolic/diastolic blood pressure data file into a 2D numeric array:

sys_dia_bp.txt
BP data
51 85
88 141
67 95
77 111
68 115
99 171
80 121

A function known as fscanf can be used for this purpose. There are three arguments to fscanf:

  1. fid1 - File identifier
  2. ’%d %d’ - Format specifier
  3. [2 inf] - Output array size

Here is an example of a script the operator may wish to right using the fscanf function:

% open file
fid1 = fopen('sys_dia_bp.txt', 'r');

% skip header line
line = fgetl(fid1);

% read systolic/diastolic b.p.
data = fscanf(fid1, '%d %d', [2 inf])

% close file
fclose(fid1);

Format specifiers

There are many different format specifiers:

Specifier Field Type
%d Signed decimal integer
%u Unsigned decimal integer
%f Floating point number
%c Character
%s String (i.e. sequence of characters)

A more complex fscanf example

If the operator is presented with strings accompanying the data in his file, he needs to separate the information. e.g.

sys_dia_bp2.txt
BP data
Sys 51 Dia 85
Sys 88 Dia 141
Sys 67 Dia 95
Sys 77 Dia 111
Sys 68 Dia 115
Sys 99 Dia 171
Sys 80 Dia 121

In his script, when using the fscanf function, if the operator includes the * symbol before after % in the format specifier then it tells Matlab to read the string but not include it in the output array. e.g.

% open file
fid = fopen('test.txt');

% skip header line
line = fgetl(fid);

% read systolic/diastolic blood pressure
data = fscanf(fid, '%*s %d %*s %d', ... [2 inf])

% close file
fclose(fid)

Summary of all I/O functions

Function File Type Operation Open/Close?
load MAT Read Matlab variables N
save MAT Write Matlab variables N
csvread Text Read comma separated value numeric data from file into 1D or 2D array N
csvwrite Text Write 1D/2D array of numeric data to file in comma separated value format N
dlmread Text Read delimiter separated value numeric data from file into 1D/2D array N
dlmwrite Text Write 1D/2D array of numeric data to file in delimiter separated value format N
fscanf Text Read dat of single type from file into 1D or 2D array Y
fgetl Text Read line of text into a string variable Y
feof Text Checks for end-of-line condition Y

The rest of this chapter will be completed soon


return  link
Written by Tobias Whetton