File Exchange

image thumbnail


version 1.1 (2.31 KB) by

Provides a one-call interface to read/write flat text files or line-based text files.



View License

This convenience function provides one-call read/write access to a flat text or line-based text file using calls of the following form.

content = textfile(filename);
textfile(filename, content);

The content can be returned as a char array, or a cell array of lines in the file. Some additional options are provided (see help).

Comments and Ratings (2)

Ben Mitch

Ben Mitch (view profile)

Thanks Jan, have copied your text into my TODO file :)

Jan Simon

Jan Simon (view profile)

This program is useful for a very frequently used job.
The documentation is exact and descriptive, but does not follow the standard design of Matlab:
function XY
% H1-line
% Help text...
This impedes e.g. LOOKFOR. The documentatoion mentions author and date. The code is well commented. Inputs are checked.

"~exist('arg1')" is not efficient to test, if an input was defined: besides variables, it accepts "arg1.m", "arg1.p", "arg1.mex...", "arg1.mdl", a folder or java class called "arg1" anywhere in the Matlab path also! Therefore it is horribly slow, if "arg1" is not a variable. Either use "exist('arg1', 'var')", or NARGIN.
The conversion from a cell string to a string without pre-allocation gets very slow for large data. SPRINTF('%s\n', content{:}) is much faster.
Pre-allocation would be a good idea for splitting a read text to a cell string also: content = cell(N, 1);
Finding CHAR(10) using "lf = find(str == 10);" has drawbacks: 1) It compares different types. 2) It creates a temporary LOGICAL array. Therefore this is ~30% faster for a 10kB string: "lf = find(str == char(10));", and "lf = findstr(str, char(10))" consumes just the half processing time. Same for "content(content ~= 13);".
Nevertheless, TEXTSCAN(fid, '%s', 'delimiter', '\n') is much more efficient to read a file into a cell string, so there is no need to reinvent the wheel.
Perhaps these ideas help to improve the efficiency of this submission.



Linux implementation was broken, now is fixed.

MATLAB Release
MATLAB 7.10 (R2010a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today