I have created a function called DISPN that answers this question.
Help displaying a formatted matrix to the command window
122 views (last 30 days)
Show older comments
Brenden Epps on 5 Jun 2012
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?
>> x = rand(3,2) - 0.5
>> format long >> x
How do I show x with, say, 6 digits of precision (as in below)??
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?
More Answers (2)
Oleg Komarov on 5 Jun 2012
Please read more carefully the documentation of sprintf(), it DOES the trick:
Now, if you wanna make the number of columns adjust automatically, then:
A = randn(3,9);
Unfortunately, there's some learning curve in format customization.
Stephen23 on 10 Oct 2018
Edited: Stephen23 on 10 Oct 2018
@Anton: yes, all of the above examples should transpose the array, like this:
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).
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!