Counting the numbers of times a number appears in a list
이전 댓글 표시
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.
Nada Ismail
2013년 6월 6일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!