Transcript Document

fprintf and other examples
Save command
>> !ls
>> a = 3;
>> save afile a
>> !ls
afile.mat
>> !dir
Directory of C:\MATLAB6p5\work\save
11/21/2003 09:30 AM
11/21/2003 09:30 AM
11/21/2003 09:30 AM
1 File(s)
<DIR>
.
<DIR>
..
184 afile.mat
184 bytes
Load Command
>> clear a;
>> whos
>> load afile.mat
>> whos
Name
Size
a
1x1
Bytes Class
8 double array
Grand total is 1 element using 8 bytes
Load/save entire workspace
>> v = 1:5;
>> m = ones(2, 3);
>> who
Your variables are:
a m v
>> save allfile
>> !ls
afile.mat
allfile.mat
>> clear
>> whos
>> load allfile.mat
>> who
Your variables are:
a m v
Ascii Format
>> save -ascii allfile.dat
>> !more allfile.dat
3.0000000e+000
1.0000000e+000 1.0000000e+000 1.0000000e+000
1.0000000e+000 1.0000000e+000 1.0000000e+000
1.0000000e+000 2.0000000e+000 3.0000000e+000 4.0000000e+000 5.0000000e+000
>> clear
>> load -ascii allfile.dat
??? Error using ==> load
Number of columns on line 2 of ASCII file C:\MATLAB6p5\work\save\allfile.dat
must be the same as previous lines.
>> who
>> m = ones(2, 3);
>> save -ascii m.dat
>> clear
>> load -ascii m.dat
>> m
m=
1 1 1
1 1 1
File Example
filename = input('Enter file name: ','s');
out_array = randn(1,10000);
% Open the output file for writing.
[fid,msg] = fopen(filename,'w');
% Was the open successful?
if fid > 0
% Write the output data.
count = fwrite(fid,out_array,'float64');
disp([int2str(count) ' values written...']);
% Close the file
status = fclose(fid);
else
% Output file open failed. Display message.
disp(msg);
end
fopen
fid = fopen(filename,permission) opens the file filename in the mode specified
by permission. permission can be:
'r‘ Open file for reading (default).
'w‘ Open file, or create new file, for writing; discard existing contents, if any.
'a‘ Open file, or create new file, for writing; append data to the end of the file.
'r+‘ Open file for reading and writing.
'w+‘ Open file, or create a new file, for reading and writing; discard existing
contents, if any.
'a+‘ Open file, or create new file, for reading and writing; append data to the
end of the file.
Reading Data Back
% Now try to recover the data. Open the file for reading.
[fid,msg] = fopen(filename,'r');
if fid > 0
% Read the input data.
[in_array, count] = fread(fid,[100 100],'float64');
disp([int2str(count) ' values read...']);
% Close the file
status = fclose(fid);
else
% Input file open failed. Display message.
disp(msg);
end
fread function
[A, COUNT] = FREAD(FID,SIZE,PRECISION) reads
binary data from the specified file and writes it into matrix
A. Optional output argument COUNT returns the number
of elements successfully read.
FID is an integer file identifier obtained from FOPEN.
The SIZE argument is optional; if not specified, the entire
file is read; if specified, valid entries are:
N
read N elements into a column vector.
inf read to the end of the file.
[M,N] read elements to fill an M-by-N matrix, in
column order. N can be inf, but M can't.
fprintf example
>> fid = fopen('grades', 'w');
>> fprintf(fid, '%d', randperm(40));
>> fclose(fid);
>> !more grades
2431821251527303926229221619361033735
4382831140143261223937520181334171
fprintf example
>> fid = fopen('grades', 'w');
>> fprintf(fid, '%d ', randperm(40));
>> fclose(fid);
>> !more grades
2 15 10 11 28 13 9 32 29 27 35 17 4 8 38 21 6
30 14 37 34 20 40 24 26 3 12 39 16 36 7 31
25 23 5 1 33 19 18 22
fscanf example
>> fid = fopen('grades', 'r');
>> grs = fscanf(fid, '%d', [1 40])
grs =
Columns 1 through 13
2 15 10 11 28 13 9
35 17 4
…
>> grs = fscanf(fid, '%d', [5 4])
grs = []
32
29
27