Printing matrix to display error message, why?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Antworten (2)
ME
am 16 Dez. 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?
0 Kommentare
Stephen23
am 16 Dez. 2019
Bearbeitet: Stephen23
am 16 Dez. 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|
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!