MATLAB: Index exceeds matrix dimensions error after code finishes

errorindex-errormatrix

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

Best Answer

In your situation, strcmp() instead of == would not be incorrect, but it is not the reason for failure.
The problem is in how you detect the end of the string. You are currently doing
if Code(counter) == ''
which assumes two things:
  1. that at the end of Code there will somehow be an empty string that neither you nor the user explicitly put there; and
  2. that you can compare to the empty string using ==
Comparing to an empty array using one of the relational operators always returns false in MATLAB. Using if strcmp(Code(counter), '') would at least remove the problem about comparing to the empty array, but it would not remove the question of how that empty array got there.
In MATLAB, there is no empty array and no byte of binary 0 at the end of a string. If you run off the end of the array then you get index exceeds matrix dimension.
You should change your code from using that while to using a for over length() of the string.