Ask for Data Extraction

8 views (last 30 days)
Blaze Dexter
Blaze Dexter on 7 Mar 2021
Commented: Blaze Dexter on 7 Mar 2021
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
Walter Roberson
Walter Roberson on 7 Mar 2021
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 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!