Having trouble with a question using arrays

The questions is:
Use the function checkupc to classify and print the total number of UPC entries stored in the array codes that are valid, invalid or missing digits. Delete all rows from the array codes that have an invalid UPC or missed digits.
The code for checkupc is:
function check = checkupc(upc)
[m n] = size(upc);
if m == 1 && n == 12
y = mod(sum([upc(2) upc(4) upc(6) upc(8) upc(10) upc(12)])+3*sum([upc(1) upc(3) upc(5) upc(7) upc(9) upc(11)]),10)
if y == 0
check = 1
else
check = 0
end
else c = 12-n check = -c end
The question beforehand asked:
The file upcs.txt contains a list of UPC codes that were scanned in a grocery store. Each line should, ideally, contain 12 digits corresponding to a single product. Read the contents of the file and store the entries into an m x 12 sized numeric array named codes, where m is the number of valid lines in the le. Lines that have less or more than 12 digits should be discarded. Some lines with 12 digits may have digits that were not correctly scanned, which were replaced by the letter X'. These missing digits should be represented in the array codes by the integer-1'. After processing the file, print the total number of lines read, the number of lines discarded, and the number of lines correctly processed and stored in codes.
upcs.txt:
X9096X082489
921642004330
810905023006
733554287763
413527622XX1
287X35871528
100093334850
764491079X90
1537X8886614
086755751640
860053705316
980098819206
038356338621
577577248178
82825685985
684580785580
736657539753
71113617151
935014271064
702345843488
58316491755
110118383664
333841856254
996003013296
495258095746
4457870230
684104168936
522784039910
6504512835
699553963094
853110488363
554147120089
The code for the problem above is:
C = textscan(fid,'%s');
C = C{1};
all_codes_num = size(C,1);
codes_discarded_num = 0;
codes_missed_digit_num = 0;
codes_correct_num = 0;
codes = [];
for i = 1:all_codes_num
one_code = C{i};
if length(one_code) == 12
x_flag = 0;
code_tmp = zeros(1,12);
for j = 1:12
if one_code(j) == 'X' || one_code(j) == 'x'
code_tmp(j) = -1;
x_flag = 1;
else
code_tmp(j) = str2num(one_code(j));
end
end
if x_flag == 1
codes_missed_digit_num = codes_missed_digit_num +1;
end
codes = [codes;code_tmp];
elseif length(one_code) ~= 12
codes_discarded_num = codes_discarded_num + 1;
end
end
all_codes_num
codes_discarded_num
codes_with_x = codes_missed_digit_num
correct_codes_without_x = all_codes_num - codes_discarded_num - codes_with_x

Respuestas (0)

La pregunta está cerrada.

Preguntada:

el 15 de Mzo. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by