MATLAB Answers

Ask for Data Extraction

36 views (last 30 days)
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 :)

Accepted Answer

Walter Roberson
Walter Roberson on 7 Mar 2021
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 Comments
Blaze Dexter
Blaze Dexter on 7 Mar 2021
ah i see, the readtables is not in r2012b, thank you so much

Sign in to comment.

More Answers (1)

Ive J
Ive J on 7 Mar 2021
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

Products


Release

R2012b

Community Treasure Hunt

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

Start Hunting!