How to use fprintf function.

조회 수: 8 (최근 30일)
Dog
Dog 2014년 1월 27일
댓글: Amit 2014년 1월 28일
I have been struggling with this problem all weekend. I am a very new user to MATLAB, and have a problem that I can't figure out. The problem is create a function that accepts a matrix of any dimension and prints the structure of the matrix.
Sample:
A=[5 4 5;3 1 6;1 2 3];
matrixPrint(A)
- -
| 5 4 5 |
| 3 1 6 |
| 1 2 3 |
- -
B=[3;2;1];
matrixPrint(B)
- -
| 3 |
| 2 |
| 1 |
- -
C=3;
matrixPrint(C)
- -
| 3 |
- -
I can figure out how to create the 3x3 matrix, but i cant figure out how to apply this function to any size matrix. Any help is greatly appreciated.
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2014년 1월 27일
What is matrixPrint ?
Walter Roberson
Walter Roberson 2014년 1월 27일
If you show your code then we can advise you on what you should change.

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

답변 (4개)

Image Analyst
Image Analyst 2014년 1월 28일
I'd use a for loop and fprintf(). Hint:
fprintf('| ');
fprintf('%d ', A(1,:)); % Use %f if A is not integer.
fprintf(' |\n');

Amit
Amit 2014년 1월 27일
I am not gonna give you the solution (I will and many others like walter will correct your code, once you made an attempt). If I was given this problem, I would use some of these functions. You can see MAtlab documentation regarding these and then try writing it.
num2str , repmat and disp
Although this can be done by many approaches. Once you try some code, post it in your question (edit you question). Even if the code does not give you what you want, still we'll direct you modify it to get the right result. Matlab is not that difficult (overstatement), you just gotta try and learn :)
  댓글 수: 2
Walter Roberson
Walter Roberson 2014년 1월 27일
I would not use num2str() or disp() for this myself. But repmat() and horzcat() and fprintf() and transpose() and size()
Amit
Amit 2014년 1월 27일
Like I said there can be many approaches. The ultimate result will be the same. I prefer disp for simple cases where I do not have to worry to much about formatting.
Once he posts a code, I would love to see your code though. Always something to learn from Matlab Answers.

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


Dog
Dog 2014년 1월 28일
Thanks for the help guys, but I figured it all out. It was tough, but I learned a lot.
  댓글 수: 1
Amit
Amit 2014년 1월 28일
Isn't that fun! Figuring out how to do it with minimum help!

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


Dog
Dog 2014년 1월 28일
This is my code:
function [ M1 ] = matrixPrint( M ) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[row,col]=size(M);
if col<=1 fprintf(' -') for i=1:3*col fprintf(' ') end fprintf('- \n') for a=1:row fprintf('| ') for j=1:col fprintf('%2.0i ',M(a,j)) end fprintf('|\n') end
fprintf(' -')
for n=1:3*col
fprintf(' ')
end
fprintf('- \n')
elseif col>1
fprintf(' -')
for i=1:2*col
fprintf(' ')
end
fprintf(' - \n')
for a=1:row
fprintf('| ')
for j=1:col
fprintf('%2.0i ',M(a,j))
end
fprintf(' |\n')
end
fprintf(' -')
for n=1:2*col
fprintf(' ')
end
fprintf(' - \n')
else end
end
  댓글 수: 1
Image Analyst
Image Analyst 2014년 1월 28일
편집: Image Analyst 2014년 1월 28일
Looks like a straightforward application of the code I gave you, except you made it a lot more complicated than it needed to be, like looping over columns instead of doing it the way I suggested. But still it looks like my hint was the catalyst that let you solve it, so can you mark my answer as accepted?

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by