Printing matrix to display error message, why?

조회 수: 2 (최근 30일)
The Legend
The Legend 2019년 12월 16일
편집: Stephen23 2019년 12월 16일
Can someone tell me why this error occurs please?
Input
M = 0 0 0 0 0 0 0
2 1 1 0 0 0 0
2 2 2 2 1 2 1
2 1 1 1 2 1 1
1 1 2 2 2 2 1
1 1 1 1 2 2 2
Function
function printMatrix(M)
[rows, cols] = size(M);
sep_row = [repelem('-', cols*2+1), '\n'];
fprintf(sep_row);
for r = 1:rows
fprintf('|');
for c = 1:cols
if M(r,c) == 1
char = 'x';
elseif M(r,c) == 2
char = 'o';
else
char = ' ';
end
fprintf([char, '|']);
end
fprintf('\n');
end
fprintf(sep_row);
end
Error output
Error using printMatrix
Too many output arguments.

답변 (2개)

ME
ME 2019년 12월 16일
I just copied and pasted your code exactly as it appears above and it runs absolutely fine for me.
Can you provide any more details that might help us understand where this is failing for you?

Stephen23
Stephen23 2019년 12월 16일
편집: Stephen23 2019년 12월 16일
The error message "Error using printMatrix Too many output arguments" tells us that you tried to call the function printMatrix with an output argument.... but the function printMatrix you showed in your question does not have any outputs, so trying to do this will throw an error.
Here is a simpler way of generating the printed character matrix:
>> Z = repmat('|',6,15);
>> V = ' xo';
>> Z(:,2:2:end) = V(M+1)
Z =
| | | | | | | |
|o|x|x| | | | |
|o|o|o|o|x|o|x|
|o|x|x|x|o|x|x|
|x|x|o|o|o|o|x|
|x|x|x|x|o|o|o|

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by