No BSD License  

Highlights from
printf, stdout, stderr

3.4

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

printf, stdout, stderr

by

 

05 May 2004 (Updated )

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

| 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)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (9)
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)

11 Sep 2009 julien diener  
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??

25 Jun 2008 Singanallur Venkatakrishnan

Goo stuff

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!

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.

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?

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.

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');

Contact us