Ask for Data Extraction

조회 수: 12 (최근 30일)
Blaze Dexter
Blaze Dexter 2021년 3월 7일
댓글: Blaze Dexter 2021년 3월 7일
Hello everyone, hope you have a good day today, i want to ask for help
I have a matriks around 1000x3, and the examples of some rows look like this
1 A1 Apple
1 A2 Apricot
1 A3 Avocado
2 B1 Banana
2 B2 Blackberry
2 B3 Blueberry
3 C1 Coconut
3 C2 Cherry
3 C3 Cranberry
4 D1 Dragonfruit
4 D2 Durian
4 D3 Date
I want to extract the data from column 2 and 3 based on the same value on column 1, but i can't find the solution, here is the idea of my script, sorry for the basic examples because i'm a beginner
data = fopen('dataexample.txt');
firstcolumn = data(:,1);
Var1 = data(firstcolumn == 1, :);
Var2 = data(firstcolumn == 2, :);
Var3 = data(firstcolumn == 3, :);
anyone can help me with this problem will be greatly appreciated. is it possible to generate a new file (in this case a txt file) for each same values in column 1? thank you for the attention, may you all always be healthy :)

채택된 답변

Walter Roberson
Walter Roberson 2021년 3월 7일
T = readtable('dataexample.txt', 'readvariablenames', false);
G = findgroups(T{:,1});
grouped = splitapply(@(var1,var2,var3) {table(var1, var2, var3)}, T, G);
for K = 1 : numel(grouped)
thistable = grouped{K};
groupid = thistable{1,1};
filename = sprintf('example_grouped_%d.txt', groupid);
writetable(thistable, filename, 'writevariablenames', false);
end
  댓글 수: 5
Walter Roberson
Walter Roberson 2021년 3월 7일
Then you need a completely different approach.
fid = fopen('dataexample.txt', 'r');
datacell = textscan(fid, '%f%s%s');
fclose(fid);
C1 = datacell{1}; C2 = datacell{2}; C3 = datacell{3};
group_ids = unique(C1, 'stable');
for K = 1:numel(group_ids)
groupid = group_ids(K);
idx = find(C1 == groupid);
filename = sprintf('example_grouped_%d.txt', groupid);
fid = fopen(filename, 'wt');
for row = idx(:).' %force row vector
fprintf('%g %s %s\n', C1(row), C2{row}, C3{row});
end
fclose(fid);
end
Blaze Dexter
Blaze Dexter 2021년 3월 7일
ah i see, the readtables is not in r2012b, thank you so much

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

추가 답변 (1개)

Ive J
Ive J 2021년 3월 7일
t = readtable('text.txt');
t.Properties.VariableNames = "col" + (1:size(t, 2)); % set col names
12×3 table
col1 col2 col3
____ ______ _______________
1 {'A1'} {'Apple' }
1 {'A2'} {'Apricot' }
1 {'A3'} {'Avocado' }
2 {'B1'} {'Banana' }
2 {'B2'} {'Blackberry' }
2 {'B3'} {'Blueberry' }
3 {'C1'} {'Coconut' }
3 {'C2'} {'Cherry' }
3 {'C3'} {'Cranberry' }
4 {'D1'} {'Dragonfruit'}
4 {'D2'} {'Durian' }
4 {'D3'} {'Date' }
The question for you would be, what do you wanna exactly do with data itself? You simply can loop over col1, or more efficiently use groupfilter
ucol1 = unique(t.col1, 'stable');
Vars = ({});
for i = 1:numel(ucol1)
Vars{i, 1} = t{t.col1 == ucol1(i), 2:end};
end

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2012b

Community Treasure Hunt

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

Start Hunting!

Translated by