Mastering MATLAB's fprintf Function: A Comprehensive Guide

In summary, the conversation discussed the use of the fprintf function in MATLAB, specifically for printing to the screen and to a file. The \f and \b escape codes were mentioned, with some uncertainty about their purpose. It was noted that \f is used for form feed and \b for backspace, but their effects on a computer monitor may differ from a printer. The conversation also referenced some documentation on fprintf and offered to share MATLAB files for practice.
  • #1
Dell
590
0
i am learning MATLAB and last week we learned the fprintf function, both for printing to the screen and for printing to file, though we only practised printing to screen,
reading over my notes i see the \f -form feed- and \b -backspace- functions, and cannot figure out what they do,,, i tried looking for them in the help file but didnt help.

i tried making a .txt file and using fprintf to write to it, then tried using \f and \b, but all they did was make a gap at the end of my previous text and then added whatever new text i wrote,, if that is what they do then how are they different to \t?
 
Physics news on Phys.org
  • #2
I don't know much about MATLAB, but the fprintf function is very similar to the function of the same name from ANSI C, which I am quite familiar with. In C the fprintf function does formatted printing to a file, and since a computer display is significantly different from a printer, there are some differences. The \f and \b escape codes are form-feed and backspace (as you said). I seem to remember that \f was used to move the paper in a printer so that the next output started on a new page. If I'm remembering correctly it didn't have that much effect on a computer monitor. The \b character was supposed to move the print head back one character.

Here's a link to some documentation on fprintf as used in MATLAB. Hope that is helpful.
Mark
 
  • #3
Dell said:
i am learning MATLAB and last week we learned the fprintf function, both for printing to the screen and for printing to file, though we only practised printing to screen,
reading over my notes i see the \f -form feed- and \b -backspace- functions, and cannot figure out what they do,,, i tried looking for them in the help file but didnt help.

i tried making a .txt file and using fprintf to write to it, then tried using \f and \b, but all they did was make a gap at the end of my previous text and then added whatever new text i wrote,, if that is what they do then how are they different to \t?

Mark44 said:
I don't know much about MATLAB, but the fprintf function is very similar to the function of the same name from ANSI C, which I am quite familiar with. In C the fprintf function does formatted printing to a file, and since a computer display is significantly different from a printer, there are some differences. The \f and \b escape codes are form-feed and backspace (as you said). I seem to remember that \f was used to move the paper in a printer so that the next output started on a new page. If I'm remembering correctly it didn't have that much effect on a computer monitor. The \b character was supposed to move the print head back one character.

Here's a link to some documentation on fprintf as used in MATLAB. Hope that is helpful.
Mark

IIRC, MATLAB's fprintf command is supposed to have the same functionality as in C. Regarding those particular control characters, however:

Form feed \f:
http://en.wikipedia.org/wiki/Form_feed

Backspace \b (when you sea^H^H^H see ^H on the internet, it's an (old) geek joke meaning that they wanted to delete a character or few before you could see it):
http://en.wikipedia.org/wiki/Backspace
 
  • #4
I took an aerodynamics class last year and my professor gave us a few projects to get us familiar with MATLAB writing abilities.

I can send you these files if you would like. I just need an email to send it to. Thanks.
 

1. What is the syntax for using the fprintf function in MATLAB?

The syntax for using the fprintf function in MATLAB is:
fprintf(fileID,format,A)
where fileID is the file identifier, format specifies the format of the output, and A is the data to be written.

2. How do I print specific data types using the fprintf function in MATLAB?

You can specify the data type by including a conversion character in the format string. For example, %d for integers, %f for floating-point numbers, and %s for strings. You can also specify the precision and width of the output using the format string.

3. Can I use the fprintf function to write data to a file in MATLAB?

Yes, the fprintf function can be used to write data to a file in MATLAB. You need to provide the file identifier as the first input argument, and the data to be written as the third input argument.

4. How do I display the output of the fprintf function in the Command Window?

In order to display the output of the fprintf function in the Command Window, you need to include the format string in the first input argument, and use the Command Window file identifier (1) as the second input argument. For example, fprintf(1, 'Hello World!');

5. Is there a limit to the number of characters that can be written using the fprintf function in MATLAB?

Yes, there is a limit to the number of characters that can be written using the fprintf function in MATLAB. The maximum number of characters that can be written is platform-dependent, but it is typically around 2048 bytes.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
754
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
820
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
Back
Top