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

  • Context: MATLAB 
  • Thread starter Thread starter GiuseppeR7
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around the use of the fprintf command in MATLAB for writing results to a file. Participants seek to understand the mechanics of this command, including related functions like fopen and fclose, within the context of homework assignments involving MATLAB scripts.

Discussion Character

  • Technical explanation, Homework-related

Main Points Raised

  • One participant expresses difficulty in understanding how to use fprintf in MATLAB for file writing.
  • Another participant provides a MATLAB example demonstrating the use of fprintf along with fopen and fclose, asking for clarification on which part is confusing.
  • A participant inquires specifically about the functions fopen and fclose, seeking to understand their roles in file handling.
  • Another participant explains the general necessity of opening files to obtain a file handle for processing and the importance of closing files to flush remaining data and manage system resources.
  • One participant shares their background with a different programming environment (Wolfram) and acknowledges the learning curve associated with file handling in MATLAB.

Areas of Agreement / Disagreement

The discussion includes varying levels of understanding regarding file handling in MATLAB, with some participants providing explanations while others express confusion. No consensus is reached on the overall understanding of fprintf and related commands.

Contextual Notes

Participants have different backgrounds in programming, which may affect their understanding of file handling concepts. There is an implied assumption that familiarity with basic programming concepts varies among participants.

Who May Find This Useful

Students or individuals learning MATLAB, particularly those transitioning from other programming languages or environments.

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
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
9K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
2
Views
3K