3.4

3.4 | 5 ratings Rate this file 136 downloads (last 30 days) File Size: 814 Bytes File ID: #4871

printf, stdout, stderr

by Gerald Dalley

 

05 May 2004 (Updated 10 May 2004)

No BSD License  

Convenience functions for text output (printf, stdout, and stderr).

Download Now | Watch this File

File Information
Description

I find that I often want to either print control characters or print out prettier output than can easily be done with just the built-in disp function. I've written a wrapper around fprintf that outputs to stdout and included stdout and stderr functions. What this allows you to do is replace code like:
  fprintf(1, 'My result is: %d', foo);
with cleaner code like
  printf('My result is: %d', foo);

The code has been tested on MATLAB 12.1 and 13, but I'd have a hard time believing it doesn't work on much older versions.

MATLAB release MATLAB 6.1 (R12.1)
Zip File Content  
Other Files stderr.m,
stdout.m,
printf.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (9)
24 May 2004 Mixel Plick

You must not unaware that if you are printing to stdout with fprintf, you can omit the file number, e.g.,

fprintf('This is going to stdout\n');

16 Feb 2006 Gerald Dalley

Mixel Plick is indeed correct.
  fprintf('foo')
is the same as
  printf('foo') % downloaded here
  fprintf(stdout, 'foo'); % downloaded here

I'm not removing this file because the download rate to me indicates that many other people have missed the fact that fprintf is overloaded to work like printf. stdout and stderr can also be useful for self-documentation purposes for some people. For most people, you'll want to follow Mixel's suggestion and just use fprintf.

10 Jan 2007 Ambarish Mitra

I have a simple question:

fprintf(stdout,"stdout");
fprintf(stderr,"stderr");

In this, I expect that first "stdout" will be printed, then "stderr". However, I observe the opposite.

Can you please explain the output?

07 Jun 2007 Gerald Dalley

Response to Ambarish Mitra:

On all versions that I use, 'stdout' prints before 'stderr', though I'm not especially surprised to see it happen the other way. In most languages, stdout is buffered for efficiency purposes and stderr is not buffered since you typically want to know about errors right away. This means that sometimes stderr messages that were submitted later will print out on the screen before later stdout messages. In C/C++, Perl, etc. it's common to use fflush(stdout) whenever these issues arise. I don't see an fflush function in Matlab or a description of its buffering behavior in the documentation, but I suspect that's what your seeing.

06 Aug 2007 David S

Thanks! Very handy for me, especially when I just want to run through a bunch of arrays and print out nicely formatted summaries of them without having to do some workaround with fprintf. Well done!

25 Jun 2008 Singanallur Venkatakrishnan

Goo stuff

25 Aug 2008 A B

you are using matlab inbuilt fprintf function
to diaplay the output data to cmd window,can you able to write your own fprintf function??

11 Sep 2009 julien diener  
11 Sep 2009 julien diener

Sorry but it is not usefull as this is exactly what fprintf is already doing.

However, it could be nice to be able to catch everything that is written in the command window without using evalc. If you have an idea, please submit it. (for example with a nice overload of fprintf)

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
strings Gerald Dalley 22 Oct 2008 07:18:59
printf Gerald Dalley 22 Oct 2008 07:18:59
stdout Gerald Dalley 22 Oct 2008 07:18:59
string manipulation Gerald Dalley 22 Oct 2008 07:18:59
utilities Gerald Dalley 22 Oct 2008 07:18:59
stderr Gerald Dalley 22 Oct 2008 07:18:59
printf Vicente 10 May 2009 06:36:31
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com