Free format read - ffread.m
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
引用格式
Miroslav Balda (2024). Free format read - ffread.m (https://www.mathworks.com/matlabcentral/fileexchange/9034-free-format-read-ffread-m), MATLAB Central File Exchange. 检索来源 .
MATLAB 版本兼容性
平台兼容性
Windows macOS Linux类别
标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!版本 | 已发布 | 发行说明 | |
---|---|---|---|
1.0.0.0 | Improved code and help, screenshot added |