Help displaying a formatted matrix to the command window

Hi all, this seems like a simple question, but sprintf(...) doesn't do the trick. Is there an EASY way to display a matrix in the command window with a specified number of digits of precision?
EXAMPLE:
>> x = rand(3,2) - 0.5
x =
-0.3810 -0.1596
-0.0016 0.0853
0.4597 -0.2762
>> format long >> x
x =
-0.381002318441623 -0.159614273333867
-0.001635948017857 0.085267750979777
0.459743958516081 -0.276188060508863
How do I show x with, say, 6 digits of precision (as in below)??
-0.381002 -0.159614
-0.001635 0.085267
0.459743 -0.276188
I'm sure one could write a code that figured out the size of x and then used sprintf to create the desired output. Does this code exist?

 Accepted Answer

More Answers (2)

Please read more carefully the documentation of sprintf(), it DOES the trick:
sprintf('%.6f %.6f\n',rand(3,2))
Edited
Now, if you wanna make the number of columns adjust automatically, then:
A = randn(3,9);
sprintf([repmat('%10.6f',1,size(A,2)) '\n'],A)
Unfortunately, there's some learning curve in format customization.

7 Comments

Hi Oleg, thanks for your quick reply. Yes, the whole point was to automate sprintf... What can be done about the issue of negative signs? sprintf fails in that case:
A = rand(3,9);
sprintf([repmat('%.6f',1,size(A,2) '\n'],A)
??? sprintf([repmat('%.6f',1,size(A,2) '\n'],A)
|
Error: Unexpected MATLAB expression.
>> sprintf([repmat('%.6f ',1,size(A,2)),'\n'],A)
ans =
0.751267 0.255095 0.505957 0.699077 0.890903 0.959291 0.547216 0.138624 0.149294
0.257508 0.840717 0.254282 0.814285 0.243525 0.929264 0.349984 0.196595 0.251084
0.616045 0.473289 0.351660 0.830829 0.585264 0.549724 0.917194 0.285839 0.757200
>> A = rand(3,9)-0.5;
>> sprintf([repmat('%.6f ',1,size(A,2)),'\n'],A)
ans =
0.253729 -0.119554 0.067822 -0.424146 -0.446050 0.030798 0.279167 0.434011 -0.370094
0.068824 -0.030609 -0.488098 -0.162877 -0.337818 0.294285 -0.188785 0.028533 -0.334351
0.101982 -0.237029 0.154079 0.189215 0.248152 -0.049458 -0.416179 -0.271023 0.413337
I edited the second solution, now it's correct.
Thanks Oleg. I was hoping that the numbers would be aligned at the decimal place, as is the case when you display a matrix in the command window normally.
A = randn(3,9);
sprintf([repmat('%10.6f',1,size(A,2)) '\n'],A)
Have you tried it?
You can tweak the %10 to give more space between the numbers.
+1: I still prefer the REPMAT approach. And if the minus character matters, I'd use the '+' specification and replace the '+' by spaces:
A = 1 - randn(3,9); fprintf(strrep(sprintf([repmat('%+10.6f',1,size(A,2)) '\n'],A), '+', ' '));
It seems that this one transposes my matrix in the process though
@Anton: yes, all of the above examples should transpose the array, like this:
fprintf(...,A.')
The fprintf help explains that it "...applies the formatSpec to all elements of arrays A1,... An in column order, and writes the data..." (emphasis added).

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!