MATLAB How Do I Use fprintf in MATLAB to Write Results to a File?

  • Thread starter Thread starter GiuseppeR7
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
The discussion revolves around using the MATLAB command `fprintf` to write results to a file as part of a homework assignment. The user expresses difficulty understanding the command and its mechanics despite reviewing the documentation. An example is provided, demonstrating how to create a file, write formatted data to it, and then close the file. Key points include the necessity of opening a file with `fopen` to obtain a file handle for processing and the importance of closing the file with `fclose` to ensure all data is written and to manage system resources effectively. The conversation highlights the differences in file handling between MATLAB and other programming environments, particularly noting the user's prior experience with Wolfram's simpler `Export` command.
GiuseppeR7
Messages
61
Reaction score
2
Hi guys I'm doing some homeworks for my professor, the homeworks are some MATLAB scripts that consist in creating and running some functions. The professor is requesting that the results obtained must be written in a file using the command fprintf.
I have tried to understand the mechanics of the command in the documentation and in Google but i simply can not understand ANYTHING...any help?
 
Physics news on Phys.org
Here's some MATLAB documentation on it:

http://www.mathworks.com/help/matlab/ref/fprintf.html

Try the example shown in MATLAB and learn by doing:

Matlab:
x = 0:.1:1;
A = [x; exp(x)];

fileID = fopen('exp.txt','w');
fprintf(fileID,'%6s %12s\n','x','exp(x)');
fprintf(fileID,'%6.2f %12.8f\n',A);
fclose(fileID);

What part of this example is giving you trouble in understanding?
 
for example what are the commands fopen and fclose doing?
 
When you work with files in any programming language, you must first open them and get a file handle to use for processing.

Once you've completed your work then you need to close the file.

Typically during file processing, file data is buffered so that each read or write doesn't result in a real disk read or write. Instead, several lines of data are read in and buffered awaiting for the program to read them and similarly several lines of output data are buffered and written out once the buffer is full. Closing a file flushes the remaining lines to the output file.

Also many programming languages have runtime limits where no more than X number of files may be open at the same time. So you will see programs that open a file, do some processing and then close the file as opposed to opening all the files, keeping them open for the duration of the program and then closing them at the end in order to avoid the max number of files open limit.
 
ok...this is one of the answers i was looking for...i worked only with Wolfram (with the easy Export[...] command) and nothing else so i never heard about this kind of stuff :p
 
Back
Top