Thursday, 4 December 2014

How to read a formatted text (ASCII) file in Matlab?

1. If a text (ASCII) file contains data with different data types then 'load' command is not useful. Then, one option can be to use TEXTSCAN with the format specifier as shown below.


fid = fopen(fileName)
formatSpec = '%f %s %f %f'

data = textscan(fid,formatSpec,3600*1,'Delimiter',',','CollectOutput', true);

fclose(fid)

The output will be a cell array. Each cell contains each of the elements. If you...
CollectOutput - will collect the consecutive data of same type in a single cell array.
Delimiter - lets you specify the delimiter used in the text file. If do not use the 'Delimiter' option then space is treated as the delimiter.


2. Many examples are given in the Matlab link given above:


No comments:

Post a Comment