Counting the numbers of times a number appears in a list
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Andrew Newell
2013-6-6
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.
采纳的回答
Angus
2013-6-7
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).
3 个评论
Angus
2013-6-7
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!