Printing matrix to display error message, why?

2 次查看(过去 30 天)
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 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by