Saving data to a text file can be a useful way to access results of your Matlab code at a later time. One easy way to do so is via the fprintf command. Let’s run through some examples to get you familiar with the concepts.
Prior to saving data, you need to initialize a file id. This can be done using the fopen command:
fid = fopen('example.txt','w');
In this case, the file will be located in the current working directory, under the name ‘example.txt’. The ‘w’ argument allows us to create and write to the file. (Note: if fid equals -1, there was an error in opening the file). There are several other options such as ‘a’ which appends to a file. Use ‘help fopen’ for further options. Now let’s try to save some data.
Assume, we have the 2 x 3 array A = [1 2 4; 5 6 7];
To save this data to ‘example.txt’, we will store three integers (%d) across each row. They will be tab delimited (\t), and after every three integers written, we will begin again on a new line (\n). In order to do so, we might run the following:
fprintf(fid,'%d\t%d\t%d\n',A);
But when you open the file ‘example.txt’, oh no! You’ll see that the matrix has been written as [1 5 2; 6 4 7] ! During the write process, fprintf will read down each column instead of across each row. To properly display the matrix, you will need to transpose matrix A. The correct fprintf command will therefore be:
fprintf(fid,'%d\t%d\t%d\n',A');
What happens if you have an unknown sized matrix? You do not want to manually type in several ‘%d\t’ into the fprintf command, and therefore one workaround is to use the ‘size’ and ‘repmat’ functions.
[rows cols] = size(A); x = repmat('%d\t',1,(cols-1)); fprintf(fid,[x,'%d\n'],A');
In these examples we have been storing integer values, but strings (%s), characters (%c), floating point values (%f), can all be stored in similar fashion.
A = [5; 12.7]; fid = fopen('example.txt','w'); fprintf(fid,'%s\t%8.3f\n','Inches',A(1)); fprintf(fid,'%s\t%8.3f\n','Centimeters',A(2)); fclose(fid);
In this example two lines will be stored, with the first row displaying Inches 5.000 and the second line Centimeters 12.700 . The extra trailing zeros are added since we specified that the floating point number have a width of 8, and the precision be 3. At the end of this example, we also closed the file using the ‘fclose’ command. This allows the file to be written to (not read only) through other programs such as Excel or Notepad++.
The fprintf command is also used as a way to display data to the screen, and will utilize the same ‘%’ placeholders. For example:
site = 'http://www.matlabgeeks.com'; title = 'MatlabGeeks'; time = 1; fprintf('Take %d minute and check out %s\n',time, site, title);
For additional information on all the fprintf arguments, see the Mathworks website.
I have tried your code, but it didn’t work,,, “fprintf” was an invalid identifier as it said,,
Hi , I read your code and I will use it.
Thank you
thanks!
very good
Thanks for the post! It was really clear! I’m just a little confused by the last example. There are two placeholders, but three things that we want to print…?
simple and clear explanation, it solve my problems in two minutes
Thank you so much. This is very very very helpful to me and well understood. I was just confused with the Mathworks site. Your explanation was down to earth.
Thank very much
Thank you. It helped a lot. Very easy to understand the way you’ve written it.
thanx….that ws helpful.
Thank you for explaining this. I didn’t understand the mathworks site at all, and looking at codes got me more confused. I now have the tools for success!
just wanted to let you know that this solved my problem in two minutes, whereas hours of trying to find the answer in the mathworks site got me… more confused.
so thanks!