How do I use fprintf to show the entire array in one ouput statement

조회 수: 224 (최근 30일)
Josua Mensah
Josua Mensah 2017년 4월 5일
댓글: Stephen23 2025년 5월 30일
>>
A = [-4 2 -7 6 8; 4 -5 8 -1 0; 0 -4 3 1 10; -8 7 -10 5 -2];
[rows cols] = size(A);
Z = zeros(size(A));
for x = 1:1:rows
for y = 1:1:cols
if A(x,y) <= 4
Z(x,y) = A(x,y);
end
end
end
fprintf('%i\n',Z);
My ouput statement I am getting is
-4
4
0
-8
2
-5
-4
0
-7
0
3
-10
0
-1
1
0
0
0
0
-2
Instead of
-4 2 -7 0 0
4 -5 0 -1 0
0 -4 3 1 0
-8 0 -10 0 -2
  댓글 수: 1
Stephen23
Stephen23 2025년 5월 30일
A = [-4,2,-7,6,8; 4,-5,8,-1,0; 0,-4,3,1,10; -8,7,-10,5,-2];
Z = A;
Z(Z>4) = 0;
display(Z) % <- much simpler than FPRINTF
Z = 4×5
-4 2 -7 0 0 4 -5 0 -1 0 0 -4 3 1 0 -8 0 -10 0 -2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

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

채택된 답변

Jan
Jan 2017년 4월 5일
편집: Jan 2017년 4월 5일
fprintf('%i %i %i %i %i\n', Z.');
or:
fprintf('%4i %4i %4i %4i %4i\n', Z.');
Note that the matrix must be transposed, because the elements are store columnwise, but the display is written in rowwise order.
By the way: It is easier without loops:
Z = zeros(size(A));
index = (A <= 4);
Z(index) = A(index);
Or:
Z = A;
Z(Z > 4) = 0;

추가 답변 (3개)

Jay Stanley
Jay Stanley 2019년 6월 1일
Tunc Durmaz,
If you do not know the dimension of your array you can get its size and replicate a template based on those dimensions
Here's a minimum working example
matrix = magic(4) % example matrix
[mrows, ncols] = size(matrix)
outputstr = ['%' num2str(mrows) 'i '] % template for the string, you put your datatype here
outputstr = repmat(outputstr, 1, ncols) % replicate it to match the number of columns
outputstr = [outputstr '\n'] % add a new line if you want
fprintf(outputstr, matrix.') % write it
For completeness sake there is another trivial solution that does not use repmat via the loop
matrix = magic(4) % example matrix
[mrows, ncols] = size(matrix)
outputstr = ['%' num2str(mrows) 'i ']
template = ['%' num2str(mrows) 'i '] % template for the string, you put your datatype here
outputstr = template
for i = 2:mcols
outputstr = [outputstr template]
end
outputstr = [outputstr '\n'] % add a new line if you want
fprintf(outputstr, matrix.') % write it
  댓글 수: 2
Tunc Durmaz
Tunc Durmaz 2019년 6월 1일
That was very helpful, thanks!
Robert Laws
Robert Laws 2025년 5월 29일
You can exploit the fact that sprintf repeats the format. Something like this can work when the data are a vector of unknown length.
ctemp=sprintf('%3i ', someintegervector);
fprintf('This is the content of the vector: %s OK', ctemp));

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


Tunc Durmaz
Tunc Durmaz 2019년 5월 13일
What if we do not know the dimension of Z? Any way to address this in fprintf?
Cheers,

Steven Lord
Steven Lord 2025년 5월 29일
This wasn't an option when the question was originally asked, but if you wanted to capture the matrix in text exactly as it would be displayed in the Command Window you could use the formattedDisplayText function.
A = [-4 2 -7 6 8; 4 -5 8 -1 0; 0 -4 3 1 10; -8 7 -10 5 -2]
A = 4×5
-4 2 -7 6 8 4 -5 8 -1 0 0 -4 3 1 10 -8 7 -10 5 -2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
s = formattedDisplayText(A)
s =
" -4 2 -7 6 8 4 -5 8 -1 0 0 -4 3 1 10 -8 7 -10 5 -2 "
Let's say I wanted to cut the spacing between elements in half, replacing two spaces with one. I could manipulate the string s.
s2 = replace(s, " ", " ")
s2 =
" -4 2 -7 6 8 4 -5 8 -1 0 0 -4 3 1 10 -8 7 -10 5 -2 "
Or I could add commas after each number except the last:
s3 = regexprep(s, "(\d) ", "$1,")
s3 =
" -4, 2, -7, 6, 8 4, -5, 8, -1, 0 0, -4, 3, 1, 10 -8, 7, -10, 5, -2 "

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by