Counting the numbers of times a number appears in a list

1 visualizzazione (ultimi 30 giorni)
Hello,
I have the following code:
clear;close;clc;
FName='PSANumbers2006.xlsx';
filename='August2006.txt';
f=fopen(filename);
line1=fgetl(f);
res=[];
k=0;
G=xlsread(FName);
h=length(G);
while ischar(line1)
if ischar(line1)
res =char(res,line1);
idx=regexp(line1,'R[1-9]');
for j=2:h;
p=G{j};
if line1(idx(1))==p
k=k+1;
out{k}=line1;
end
end
line1 = fgetl(f);
end
Pair={out,k};
end
xlsappend('Report',Pair);
The File "PSANumbers2006.xlsx" holds a list similar to this:
PSA Numbers
R00763
R00766
R00759
R00761
R00767
R00768
R00764
R00757
R00879
R00877
R00881
R00880
R00886
R00849
R00818
R00817
R00860
...
and the file "August2006.txt" holds the following information:
PSA_num Client Name Start_Time End_time Category
R00818 Counseling Center of Milwaukee Inc 8/1/2006 Educational
R00817 Milwaukee County Zoo 8/1/2006 Social
R00860 Wisconsin Humane Society 8/28/2006 Environmental
R00860 Wisconsin Humane Society 8/28/2006 Environmental
R00857 Radiology.Org Health
R00856 Clean Wisconsin 8/25/2006 Environmental
...
The code is then supposed to compare every number in the first list (A)to the numbers in the second list (B). For every time a number from list A is found in list B, the 'k' should increase. Once the code has gone through all of list A the output should be:
PSA_num Client Name Start_Time End_time Category Played
R00818 Counseling Center of Milwaukee Inc 8/1/2006 Educational 1
R00817 Milwaukee County Zoo 8/1/2006 Social 1
R00860 Wisconsin Humane Society 8/28/2006 Environmental 2
R00857 Radiology.Org Health 1
R00856 Clean Wisconsin 8/25/2006 Environmental 1
However, I get the following error:
Undefined function or variable 'out'.
Error in Ranking_Read (line 33)
Pair={out,k};
  2 Commenti
Andrew Newell
Andrew Newell il 6 Giu 2013
You were missing an END in the loop. I assume that was a typo - I added it where I think it is supposed to go.
Nada Ismail
Nada Ismail il 6 Giu 2013
Thanks, but I don't think that was the problem.

Accedi per commentare.

Risposta accettata

Angus
Angus il 7 Giu 2013
There are some problems with this, your regexp will only return an index so when trying to compare to 'p' it will always fail as it compares a singe char with a full string (eq. 'R'=='R00818'). Since 'out' is initialized within the if statement it will not be created and thus your error message. You could try replacing with
idx=regexp(line1,'R\d*','match');
as that returns the entire matched expression. There are other things that will need correcting as well (eg. your 'k' never resets).
Maybe grp2idx could be used in an entirely alternate method.
  3 Commenti
Angus
Angus il 7 Giu 2013
Great.
Also, as a curiosity/suggestion, if all of the entries in August2006.txt are known to have 'valid' PSA numbers (they are in the 2006 PSA number list) then might you be able to just compare lines within August2006.txt and look for repeated entries?
Sorry if I should have asked for this clarification at the start.
Cheers
Nada Ismail
Nada Ismail il 10 Giu 2013
It will be multiple files, such as August2006.txt, compared to PSANumbers2006.xls. I wish it was just comparing within one list.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by