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

조회 수: 3 (최근 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
Rik
Rik 2021년 2월 22일
Why did you post your code as an image? Now we can't edit it. Please replace the image by code and attach your data file.
Also, have you searched for examples how you can read csv files?

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

채택된 답변

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:
Reading the file outside a function would become more significant if this lookup is being done more than just once--that way one has to reread the file every time; this way the data are already in hand and only read once.
One could, of course, create the function with the data as persistent, but that adds the complexity of needing code to clear/recreate if the input data are to be changed at any time.
All in all, in this case it just looks cleaner to me to read the data and do what need there...
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개)

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by