Matlab Quick Reference: Input/Ouput
I/O
Extract date from file
1 2 | s = dir('junk.m'); s.date |
Open a formatted text file
1 | fid = fopen('file.dat','w') |
w: write r: read a: append returns -1 if error
If the file is a text file and you want to write to it, consider using ‘t’ as an attribute if you want Windows line break example:
1 | fid = fopen('file.txt', 'wt') |
1 | fclose(fid) |
1 | fclose('all') % closes all open files |
Filename
1 2 | [pathstr, name, ext, versn] = fileparts(filein); fileout = fullfile(pathstr, [name, '.out']); |
Write to formatted text file (line by line)
1 | fprintf(fid,'%2.4f\n',x) |
fid: 1 = stdout; 2 = stderr
- omitting fid writes on the screen
d: integer
f: fixed point notation e: exponential notation g: same as f or e but insignificant zeros do not print s: string of characters c: character Remember \n for line break
fprintf tips
Printing strings and numbers together: it looks like you have to separate the printing of strings and numbers into distinct fprintf statements. Example:
1 2 | fprintf(fid, '%s', 'test '); fprintf(fid, '%d %d\n', 10, 20); %printed on the same line as 'test ' |
Write (numeric) matrices to text file
1 2 | A = [1 2 3 4 ; 5 6 7 8]; dlmwrite('my_data.out', A, ';') |
‘;’ is the delimiter in my_data.out
“ means no delimiter
Note: not necessary to open the file first
Note: use the following notation if you want Windows line break:
1 | dlmwrite('my_data.out', A, 'delimiter', ';', 'newline', 'pc') |
To decide the number precision use the ‘precision’ attribute
1 | 'precision', '%10.5f' |
Read ASCII delimited numeric data file
1 2 | <p>data = dlmread('myfile.dat'); col1 = data(:,1);</p> <p> </p> |
Read from file containing numbers and strings
1 | textscan |
example:
Text file scan1.dat contains data in the following form:
Sally Level1 12.34 45 1.23e10 inf NaN Yes
Joe Level2 23.54 60 9e19 -inf 0.001 No
Bill Level3 34.90 12 2e5 10 100 No
Read each column into a variable:
1 2 3 | fid = fopen('scan1.dat'); C = textscan(fid, '%s %s %f32 %d8 %u %f %f %s'); fclose(fid); |
Specifiers:
%s : string
%n : double
%d : int
Note that textscan returns a cell array. Therefore C{1} is the cell array containing the surnames ‘Sally’, ‘Joe’ and ‘Bill’. C{1}(1) is the cell containing the string ‘Sally’. To convert this cell to a string (i.e. a character array), use the char function:
1 | sallyString = char(C{1}(1)); |
The conversion is necessary e.g. if you want to write out the string using fprintf
Read file containing numbers and NaN
Example:
1 | nc = 6; % number of columns of data in the input file <br />fmt = repmat('%n', 1, nc); <br />clear m; <br />[m{1:nc}] = textread(filein, fmt); <br />m = cat(2,m{:}); |
Delimiters (textscan and textread)
Default delimiter:
‘ \b\t’ To use a different delimiter:
1 | C = textscan(fid, '%s %n', 'delimiter', '\t'); |
If a nondefault delimiter is specified, repeated delimiter characters are treated as separate delimiters. When using the default delimiter, repeated white-space characters are treated as a single delimiter.
Skipping fields (textscan and textread)
Use asterisk: %*s instead of %s Particularly handy when using textread where you can write the data directly into a list of variables
Error message
1 | error('ERROR: wrong format'); |

