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.



Friday, July 15, 2016

Working with Text files in MATLAB - Part 2

In part one we discussed about basic functions to deal with text files. Now we will discuss more function to read from and write to different types of text files.

csvwrite( )  - Write comma-separated value file

csvwrite is the basic function in MATLAB to write data to a file as comma-separated values. It has only limited options to write numeric data to the file. The syntax is:
csvwrite(filename, C)
The function writes matrix C into the file as comma-separated values.

>> csvwrite('csvexample.txt',data)
Write the contents of the variable data to the file csvexample.txt as comma separated values.
If you want to write with more formatting options, you have to use fprinf or dlmwrite functions.

csvread( ) - Import Comma-Separated Data

Like csvwrite function, csvread also is simple function to read a .csv file or range of comma-separated numeric data and save to a matrix. The file must contain only numeric values.
X = csvread('filename')
Other optional arguments are offset and range.
X = csvread('filename', R, C, [range])
Where R and C are the row and column offsets and the range is row and column numbers of left top and right bottom points defined as range = [R1 C1 R2 C2].
To read the data from the csvexample.txt file we made in the previous example:
>> A=csvread('csvexample.txt')
A =
     1     2     3     4
     5     6     7     8
     9    10    11    12

dlmwrite( ) - Write matrix to ASCII-delimited file

dlmwrite function also is used to write the data in to a file. But it has more options than the csvwrite function. The syntax is:
dlmwrite('filename', C, 'delimiter', ’value’, R, C)
The function writes the contents of matrix C in to the file.  The delimiter is used to separate the values. The default delimiter is ‘,’ (comma). You can us other values also as the delimiter. The offset values R and C are optional.
There are optional user configurable attributes available to have more control over writing the data. ‘-append’ , ‘precision’, ‘newline’ are some commonly used attributes.
>> dlmwrite('dlmexample.txt',data)
Will write the contents of the variable data to the file dlmexample.txt as shown below
Now if we modify our command using different delimiter the file will be updated as:
>> dlmwrite('dlmexample.txt',data,'delimiter',';')
Note that the delimiter is changed to semi-colon.
>> dlmwrite('dlmexample.txt',data,'delimiter','\t')
1   2   3   4
5   6   7   8
9   10  11  12
Now the de limiter is the tab.
1.000000    2.000000    3.000000    4.000000
5.000000    6.000000    7.000000    8.000000
9.000000    10.000000   11.000000   12.000000
The ‘precision’ attribute is used to set the number of significant digits to 6.

dlmread( ) - Import Delimited Numeric Data

This function is used to read a file or a range of numeric data separated by any delimiter to a matrix. The file must contain numeric data only. The Syntax is:
X = dlmread(‘filename, ‘delimiter’, [range])
Delimiter and range are optional. If the delimiter argument is not specified, then the delimiter is inferred from the formatting of the file (consecutive white spaces are treated as a single delimiter). Range is to define the range of data to be read from a file, as explained in the csvread section.
>> A=dlmread('dlmexample.txt')
A =
     1     2     3     4
     5     6     7     8
     9    10    11    12
To read only 3rd row of the file:
>> A=dlmread('dlmexample.txt','\t', [2 0 2 3])
A =
     9    10    11    12

type( ) - Display contents of file

type is the function to display the contents of a file in the command window. The contents of the file may be numeric or non-numeric. The syntax is:
type ‘filename’

>> type 'dlmexample.txt'
1.000000  2.000000  3.000000  4.000000
5.000000  6.000000  7.000000  8.000000
9.000000  10.000000 11.000000 12.000000

>> type 'texttest.txt'
Welcome to Electrical Engineering Tutorial

>> type 'test.txt'
% This is a grade file
     jim 99 87 98
     jess 94 92 91

     jenna 100 90 95
submit to reddit