How to create a new array with specific column and row thresholds from txt file

2 次查看(过去 30 天)
Hello, I am trying to extract specific numeric values from a txt file to create an array so that I can do further analysis of these values. However the requirements for me are to only use the numeric values if the row starts with an A and comes from the column marked 'Value1'. I have been able to get which column to use and how to find the correct rows to use but I am having trouble getting the data into an array so that I can analyize the values. Any help would be appreciated!
fPath = 'SampleDataset01.txt';
cat = 'A';
cName = 'Value1';
fConn = fopen(fPath, 'r');
firstLine = fgetl(fConn);
new = [];
while ~feof(fConn)
cLine = fgetl(fConn);
Y = strsplit(firstLine, ',');
X = ismember(Y, cName);
V = find(X);
parts = strsplit(cLine, ',');
O = str2double(parts(V));
tru = strcmp(cat, parts{1});
end
fclose(fConn);

回答(1 个)

dpb
dpb 2021-2-22
编辑:dpb 2021-2-23
Why do you keep reverting to the hardest way possible to read in a data file? We just showed how easy it is to use readtable @ <Reading-csv-file-and-counting-number-of-lines-that-have-a-category-of-interest>, why not repeat the action?
The logic there also solves almost the whole problem again; on the likelihood that just one of the letters is unlikely to be the only one of interest in the analyses going forward, look at grouping variables and rowfun to do it all in "one swelll foop".
Whatever you do, forget this thing about trying to read a file record-by-record as text--bring the data in memory and work on it there.
>> data=readtable('SampleDataset01.txt');
>> head(data)
ans =
8×4 table
Category Value1 Value2 Value3
________ ______ ______ ______
{'C'} 6.4 3.3 5.3
{'A'} 3.3 6.5 2.8
{'C'} 6.1 8.5 7.3
{'A'} 5.3 2.6 2.5
{'A'} 3.4 5.2 4.7
{'H'} 2.8 5.5 4.1
{'C'} 4.9 4.9 2.8
{'D'} 5.9 5 4.8
>> rowfun(@mean,data,"InputVariables",'Value1','groupingvariables','Category','OutputVariableNames','Value1 Means')
ans =
8×3 table
Category GroupCount Value1 Means
________ __________ ____________
{'A'} 7 4.6857
{'B'} 3 7.1333
{'C'} 7 5.7286
{'D'} 3 4.7
{'E'} 7 5.1286
{'F'} 2 5.6
{'G'} 2 5.55
{'H'} 1 2.8
>>
Alternatively,
>> mnV1A=mean(data.Value1(contains(data.Category,'A')))
mnV1A =
4.6857
>>
Similarly as in previous, one could write a general anonymous function for the purpose, but the prepackaged tools MATLAB provides such as rowfun are so powerful, why not take advantage? It's the whole point in having a high-level programming environment such as MATLAB at one's disposal.

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by