Reading csv file and counting number of lines that have a category of interest and are below a threshold

조회 수: 29(최근 30일)
fName = 'SampleDataset03.csv';
myCat = 'A';
maxValue = 0.5;
fConn = fopen(fName, 'r');
firstLine = fgetl(fConn);
final = 0;
while ~feof(fConn)
cLine = fgetl(fConn);
parts = strsplit(cLine, ',');
F = strcmp(myCat, parts(:,1));
T = cellfun(@(x) x < maxValue, parts(:,2));
if F
if maxValue > parts(:,2)
final = final + 1;
end
end
end
fclose(fConn);
I am writing a function that reads a csv file and counts all of the lines that have an 'A' and are below 0.5. I have the general framework of the code but I am struggling with comparing the lines in the csv file with the 0.5 threshold. I have tried str2double to get a numeric value but it seems that if I use that the first column with a letter causes an error. I have tried cellfun but that still does not seem to work. Can you help?
Sample csv file:
  댓글 수: 2

댓글을 달려면 로그인하십시오.

채택된 답변

dpb
dpb 2021년 2월 22일
편집: dpb 2021년 2월 22일
>> data=readtable('SampleDataset03.csv');
>> sum(contains(data.Category,'A')&data.Value<0.5)
ans =
64
>>
For this you don't really need a separate function, just define an anonymous function in the calling routine and read the file inline as you go.
The anonymous function could be
fnCountEm=@(t,c,v) sum(contains(t.Category,c)&t.Value<v);
and use like
>> fnCountEm(data,'A',0.5)
ans =
64
>> fnCountEm(data,'X',0.25)
ans =
0
>> fnCountEm(data,'G',1.25)
ans =
119
>>
  댓글 수: 2
dpb
dpb 2021년 2월 22일
ADDENDUM SECOND:
fnCountEm=@(t,c,v) sum(contains(t.Category,c)&t.Value<v);
lets one redefine the table and call the function with new data at will; if the data are indeed unchanging, then could encapsulate it inside the anonymous function and remove from the argument list.
fnCountEm=@(c,v) sum(contains(data.Category,c)&data.Value<v); % data the table in memory
Then, of course, if the data are ever changed one must redefine the anonymous function to reflect that.

댓글을 달려면 로그인하십시오.

추가 답변(0개)

범주

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by