How do I debug this code?

4 views (last 30 days)
Edivaldo Bartolomeu
Edivaldo Bartolomeu on 13 Jan 2021
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 Comments
Athrey Ranjith Krishnanunni
Edited: Athrey Ranjith Krishnanunni on 23 Jan 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
Edivaldo Bartolomeu on 22 Jan 2021
Alright, thanks for helping mate.

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 15 Jan 2021
Edited: Cris LaPierre on 15 Jan 2021
Preallocation messages are code analyzer warnings, not errors. They are tips to improve your code, but will not prevent it from running.

Categories

Find more on Entering Commands in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!