Free format read - ffread.m

ffread.m is applicable for reading character information from strings or files.
3.1K Downloads
Updated 20 Jun 2007

View License

Free-format-read function reads data of the known structure from an input, which may be either ASCII file or a string. Individual items may be numbers, words or strings. Almost any character may serve as a delimiter. The same holds even for a decimal point in real numbers. The function recognizes types of the read data. An attempt to read item after the physical end of data is signalized by returned character 127.

Forms of a call:
~~~~~~~~~~~~~~~~

First entry:
ffread(name);
name = file name of the input file or string of data;
default delimiters of items are ' ' or 'tab' 'cr' 'lf',
default decimal point is '.'
ffread(name,delim);
delim = user's delimiter of items
ffread(name,delim,point);
point = user's decimal point character (default '.')

Other entries:
data = ffread;
data = single item = number,
word = string without a pair of single quotes
string
data = ffread(n);
n = number of items to be read,
data = column of n read items as
i. n-vector of numbers or
ii. n-cell array of strings
Example:
~~~~~~~~
Given the file 'test.txt' containing 6 lines:
% Testing example for ffread
WORD % word (string without single quotes and spaces)
'This is a test' % string
This is a cell array% multiple words = cell array
123,45e-2; 1 2; 3 % ',' will be converted into '.'
4; 5 6 % ';' will be converted into ' '

The file 'test.txt' may be read by the following script:
ffread('test.txt', ';', ','); % file name, item delim, dec. point char.
w = ffread; % reads word WORD into w as string
s = ffread; % reads string into s
c = ffread(5); % cell array of 5 words
a = ffread; % reads one real number
v = (ffread(6)).';% row vector v of 6 elements
err = ffread % error! char(127) = true data already finished

Cite As

Miroslav Balda (2024). Free format read - ffread.m (https://www.mathworks.com/matlabcentral/fileexchange/9034-free-format-read-ffread-m), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R14
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Characters and Strings in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0.0

Improved code and help, screenshot added