Index exceeds matrix dimensions error after code finishes
Mostrar comentarios más antiguos
The code runs fine and I get the answer but at the end, I get the error Index exceeds matrix dimensions. How do I fix this so it doesn't have it at the end of my code? This is the command window: Enter characters you would like translated(Enter in ALL CAPS): AZ4M6NN0S789P .- --.. ....- -- -.... -. -. ----- ... --.... ---.. ----. .--. Index exceeds matrix dimensions.
Code = input('Enter characters you would like translated(Enter in ALL CAPS): ','s');
counter=0;
n=0;
while n~=1 %fix it so that i can have it so while Code(counter) has a value it will run
counter=counter+1;
Code(counter);
%add a bunch of if statements to convert code(counter) then once it
%finds it print it out and run program again
if Code(counter)== '1'
fprintf('.---- ');
end
if Code(counter)== '2'
fprintf('..--- ');
end
if Code(counter)== '3'
fprintf('...-- ');
end
if Code(counter)== '4'
fprintf('....- ');
end
if Code(counter)== '5'
fprintf('..... ');
end
if Code(counter)== '6'
fprintf('-.... ');
end
if Code(counter)== '7'
fprintf('--.... ');
end
if Code(counter)== '8'
fprintf('---.. ');
end
if Code(counter)== '9'
fprintf('----. ');
end
if Code(counter)== '0'
fprintf('----- ');
end
if Code(counter)== 'A'
fprintf('.- ');
end
if Code(counter)== 'B'
fprintf('-... ');
end
if Code(counter)== 'C'
fprintf('-.-. ');
end
if Code(counter)== 'D'
fprintf('-.. ');
end
if Code(counter)== 'E'
fprintf('. ');
end
if Code(counter)== 'F'
fprintf('..-. ');
end
if Code(counter)== 'G'
fprintf('--. ');
end
if Code(counter)== 'H'
fprintf('.... ');
end
if Code(counter)== 'I'
fprintf('.. ');
end
if Code(counter)== 'J'
fprintf('.--- ');
end
if Code(counter)== 'K'
fprintf('-.- ');
end
if Code(counter)== 'L'
fprintf('.-.. ');
end
if Code(counter)== 'M'
fprintf('-- ');
end
if Code(counter)== 'N'
fprintf('-. ');
end
if Code(counter)== 'O'
fprintf('--- ');
end
if Code(counter)== 'P'
fprintf('.--. ');
end
if Code(counter)== 'Q'
fprintf('--.- ');
end
if Code(counter)== 'R'
fprintf('.-. ');
end
if Code(counter)== 'S'
fprintf('... ');
end
if Code(counter)== 'T'
fprintf('- ');
end
if Code(counter)== 'U'
fprintf('..- ');
end
if Code(counter)== 'V'
fprintf('...- ');
end
if Code(counter)== 'W'
fprintf('.-- ');
end
if Code(counter)== 'X'
fprintf('-..- ');
end
if Code(counter)== 'Y'
fprintf('-.-- ');
end
if Code(counter)== 'Z'
fprintf('--.. ');
end
if Code(counter) == ''
n=1
end
end
Respuesta aceptada
Más respuestas (1)
Use strcmp, not == when testing equality of strings.
e.g.
if strcmp( Code( counter ), 'Y' )
...
end
1 comentario
Jonathan Weaver
el 27 de Nov. de 2017
Categorías
Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!