How do I debug this code?
Mostrar comentarios más antiguos
Hello everyone,
I was given the code depicted below which contains 5 erros, I was able to debug the 1st and last errors or warning messages. The other warning messages are related to Preallocation of Variables, and that's where I am struggling. Your help would be appreciated.
clear all;
close all;
clc;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ask user for zip code
code = input('Please enter the Postal Bar Code: ','s');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% break the zip code into segments of 5 and convert to the appropriate
% number
zip = ''; % empty initial zip code
digit_code = ''; % empty initial digit
for k = 1:length(code) % go through all valid bars
digit_code = [digit_code code(k)]; % get next digit
if length(digit_code) == 5 % if 5 bars collected
switch digit_code % determine number from barcode
case '...II'
number = '1';
case '..I.I'
number = '2';
case '..II.'
number = '3';
case '.I..I'
number = '4';
case '.I.I.'
number = '5';
case '.II..'
number = '6';
case 'I...I'
number = '7';
case 'I..I.'
number = '8';
case 'I.I..'
number = '9';
otherwise
number = '0';
end
if count <= 8 % if value is a part of the zip code
zip = [zip number]; % add digit to
count = count + 1; % update count
digit_code = ''; % reset digit
else % if value is the error check value
error_val = number; % store error check value
end
if count == 5
zip = [zip '-']; % add dash after 5 digits
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check for errors in reading bar code
% sum zip digits
zip_sum = 0; % initialize sum
for k = 1:10
if k ~= 6
zip_sum = zip_sum + str2num(zip(k)); % add each digit
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check for error
if mod(error_val+zip_sum,10) ~= 0
fprintf('There is an error in the Postal Bar Code. Zip code cannot be determined.\n');
else
fprintf('The zip code is %i.\n',zip);
end
4 comentarios
Athrey Ranjith Krishnanunni
el 15 de En. de 2021
It would help to know the complete error/ warning messages, with line numbers and the exact text.
Edivaldo Bartolomeu
el 19 de En. de 2021
Athrey Ranjith Krishnanunni
el 20 de En. de 2021
Editada: Athrey Ranjith Krishnanunni
el 23 de En. de 2021
Well, it would have been better to know which line is 24, 49, or 56, but in this specific context, I can take a guess. In the future, please specify the offending line numbers in the code itself (as comments).
As to what to do, I don't think preallocation would be of any significant help, because the array size doesn't grow by a lot, and it would be too much trouble for you to rewrite your code to include preallocation.
You can suppress the messages by typing
%#ok<AGROW>
at the end of each offending line.
Edivaldo Bartolomeu
el 22 de En. de 2021
Respuestas (1)
Cris LaPierre
el 15 de En. de 2021
Editada: Cris LaPierre
el 15 de En. de 2021
1 voto
Preallocation messages are code analyzer warnings, not errors. They are tips to improve your code, but will not prevent it from running.
1 comentario
Edivaldo Bartolomeu
el 22 de En. de 2021
Categorías
Más información sobre Programming en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!