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 Center および File ExchangeArgument Definitions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by