A blog to help electrical engineers and students to learn electrical engineering topics. Basic Electrical Engineering, Power Systems, Electrical Machines, tec.. Now special course on MATLAB for Beginners.



Saturday, July 2, 2016

Working with Text files in MATLAB - Part 1

MATLAB can import text files both interactively and programmatically. Import Tool used to import data interactively. The Import Tool supports text files, including those with the extensions .txt, .dat, .csv, .asc, .tab, and .dlm. Data in these files can be a combination of numeric and nonnumeric text, and can be delimited by one or more characters.
There are many inbuilt functions to import and export data from and to the text files programmatically. Most of the import functions for text files require that each row of data has the same number of columns, and they allow to import a specified a range of data.
We will discuss some of the common functions to work with text files.

textread( ) - Read formatted data from text file

textread is a function which handles many of the problems/tasks of file input. textread is a function that "takes in" the name of a file and "returns" an array of information.
General syntax is
A = textread('filename', ' ', N, ’param’, value, ...)
X = textread('filename') reads numeric data from  the file into a single variable X.
Param and value are optional arguments to configure the output. One common parameter is ‘headerline’. It is used to tell textread to skip the first line of the file (the non-data line). Use the optional argument "'headerlines', number" where number is how many lines to skip.
X = textread('data', 'headerlines', 1);
There are many other types of parameters to configure the output. For the full list refer the Matlab help.
Suppose you wanted to read a file with strings in it as well as numbers. To do this, you must specify the "%format" of each column of data.
[A,B,C, ...] = textread('filename', 'format specifier', N, ’param’, value, ..) reads data from the file into the variables A,B,C,etc.  The type of each return argument is given by the format specifier string.  The number of return arguments must match the number of format specifiers in the FORMAT string.
N is another optimal argument to read a specific number of values to read from the file.  
A = textread('filename', '%d', 1); %  This reads a single integer from the file.


Consider the file test.txt, whose content is:
            % This is a grade file
            jim 99 87 98
            jess 94 92 91
            jenna 100 90 95
To read this file we use:
>> [names, grade1, grade2, grade3] = textread('test.txt', '%s %f %f %f', 'headerlines', 1)
names =
grade1 =
grade2 =
grade3 =
To read all numeric data from a delimited text file, use a single output argument, empty format string, and use the appropriate delimiter.
Suppose delimexample.txt contains:

To read the whole matrix into a single variable:
>> [data] = textread('delimexample.txt','','delimiter',',')
data =
     1     2     3     4
     5     6     7     8
     9    10    11    12    

textscan( ) – Read Formatted Data from a Nonrectangular Text File

The textscan function is used to read formatted text file and save data to a cell array. The synatax is similar to the textread function. Here to read multiple data we don’t need to specify the multiple output variables, but the whole data will be stored as a cell array. The syntax is:
C = textscan(FID, 'format specifier')
Other optional arguments are very similar to the textread function.
When textscan reads a specified file or string, it attempts to match the data to the format string. If textscan fails to convert a data field, it stops reading and returns all fields read before the failure.
Consider the test.txt file in the previous section. To read the file content to a cell array C:
>> fid = fopen ('test.txt', 'r+');
>> C = textscan(fid, '%s %f %f %f', 'headerlines', 1);
>> celldisp(C)
C{1}{1} =
 C{1}{2} =
 C{1}{3} =
 C{2} =
C{3} =
C{4} =

The textscan function can be used to read data in a string. The syntax is:
C = textscan(string)
submit to reddit