Printing matrix to display error message, why?

2 visualizzazioni (ultimi 30 giorni)
The Legend
The Legend il 16 Dic 2019
Modificato: Stephen23 il 16 Dic 2019
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.

Risposte (2)

ME
ME il 16 Dic 2019
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 il 16 Dic 2019
Modificato: Stephen23 il 16 Dic 2019
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|

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by