Ask for Data Extraction

1 回表示 (過去 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 Center および File ExchangeData Distribution Plots についてさらに検索

製品


リリース

R2012b

Community Treasure Hunt

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

Start Hunting!

Translated by