Chapter 5 - File Input and Output
Chapter 5 - File Input and Output
Chapter 5 - File Input and Output
5.1. Saving/reading binary files and making calls to the operating system
When using MATLAB, either when running a m-file or performing calculations interactively,
there is a master memory structure that MATLAB uses to keep track of the values of all of the
variables. This memory space can be written in a binary format to a file for storing the results
of your calculations for later use. This is often useful when you have to interrupt a MATLAB
session. The following commands demonstrate how to use this storage option to make binary
.mat files.
Afull=zeros(num_pts,num_pts);
Afull(1,1) = 1;
Afull(num_pts,num_pts) = 1;
Afull(i,i) = 2;
Afull(i,i-1) = -1;
Afull(i,i+1) = -1;
end
b = linspace(0,1,num_pts)';
x = Afull\b;
The "save" command saves the data in the memory space to the named binary file.
save mem_store1.mat;
clear all;
The "load" command loads the data stored in the named binary file into memory.
load mem_store1.mat;
If we want to get rid of this file, we can use the "delete" command.
delete mem_store1.mat;
ls *.mat
In the commands above, I have used path names to specify the directory. We can view our
pwd
pwd; ls
We can also use the "save" command to save only selected variables to a binary file.
save mem_store2.mat Afull;
clear all
whos
load mem_store2.mat
whos
delete mem_store2.mat
clear all
Afull=zeros(num_pts,num_pts);
Afull(1,1) = 1;
Afull(num_pts,num_pts) = 1;
Afull(i,i) = 2;
Afull(i,i-1) = -1;
Afull(i,i+1) = -1;
end
b = linspace(0,1,num_pts)';
x = Afull\b;
Now, let us write out the contents of Afull into a file that we can read.
One option is to use the "save" command with the option -ascii, that writes to a file using the
ASCII format.
save store1.dat Afull -ascii;
type store1.dat view contents of file
We can also load a file in this manner. The contents of the ASCII file filename.dat are stored in
the MATLAB variable filename. This is a good way to import data from experiments or other
programs into MATLAB.
load store1.dat;
If we add the option -double, the data is printed out with double the amount of digits for
higher precision.
delete store1.dat;
type store1.dat
We can use this command with multiple variables, but we see that no spaces are added.
delete store1.dat;
save store1.dat Afull b x -ascii;
type store1.dat view contents of file
delete store1.dat get rid of file
MATLAB also allows more complex formatted file input/output of data using commands that
are similar to those in C.
FID = fopen(FILENAME,PERMISSION)
FID_out = fopen('test_io.dat','w');
ls
Now, we print the b vector to the output file as a column vector using the "fprintf" command.
In the FORMAT string '\n' signifies a carriage return, and 10.5f specifies a floating point
decimal output with 5 numbers after the decimal point and a total field width of 10.
for i=1:length(b)
fprintf(FID_out,'10.5f \n',b(i));
end
type test_io.dat;
FID_out = fopen('test_io.dat','a');
fprintf(FID_out,'\n');
for i = 1:size(C,1)
end
fclose(FID_out);
type test_io.dat;
We now read the b vector into the variable b_new. First, we allocate space for the vector, and
then we read in the values one by one.
b_new = linspace(0,0,num_pts)';
for i=1:num_pts
b_new(i) = fscanf(FID_in,'f',1);
end
b_new
for i=1:size(C,1)
for j=1:size(C,2)
C_new(i,j) = fscanf(FID_in,'f',1);
end
end
C_new
fclose(FID_in);
clear all