필터 지우기
필터 지우기

Help displaying a formatted matrix to the command window

조회 수: 43 (최근 30일)
Brenden Epps
Brenden Epps 2012년 6월 5일
편집: Stephen23 2018년 10월 10일
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?

채택된 답변

Brenden Epps
Brenden Epps 2012년 6월 6일
I have created a function called DISPN that answers this question.

추가 답변 (2개)

Oleg Komarov
Oleg Komarov 2012년 6월 5일
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
Anton
Anton 2018년 10월 10일
It seems that this one transposes my matrix in the process though
Stephen23
Stephen23 2018년 10월 10일
편집: Stephen23 2018년 10월 10일
@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).

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2018년 10월 10일
Try
num2str(x, 6)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by