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.
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)
you are using matlab inbuilt fprintf function
to diaplay the output data to cmd window,can you able to write your own fprintf function??
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!
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.
I have a simple question:
In this, I expect that first "stdout" will be printed, then "stderr". However, I observe the opposite.
Can you please explain the output?
Mixel Plick is indeed correct.
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.
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');