fileread
Reads a whole text file into a character vector.
Category: Data Import and Analysis > Data Import and Export
Syntax
text = fileread(filename)
Returns the entire contents of the file.
Description
This is the simplest way to load a small file: no fopen and fclose pair is needed. Split the result into lines with splitlines or strsplit, or parse it with jsondecode, str2double and friends. For big files, read incrementally instead.
Input arguments
filename- Name of the file.
Output arguments
text- Character row vector holding every character, including newlines.
Example
Load and split a file
fid = fopen('f.txt', 'w');
fprintf(fid, 'red\ngreen\nblue\n');
fclose(fid);
s = fileread('f.txt');
numel(s)
splitlines(strtrim(s))
Output
ans = 15
ans = 3×1 cell
{'red' }
{'green'}
{'blue' }