Ask for Data Extraction
이전 댓글 표시
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 :)
채택된 답변
추가 답변 (1개)
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
카테고리
도움말 센터 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!