How can i match value in file and write the match corresponding row in new file

1 回表示 (過去 30 日間)
Retno Purwaningsih
Retno Purwaningsih 2021 年 8 月 15 日
コメント済み: Wan Ji 2021 年 8 月 16 日
I need to extract the matching value and corresponding row in new file
Data
Col1 Col2 Col3 Col4 Col5 Col6 Co7
222 -10.894943 127.960434 0.0136 -0.2221 0.0292 0.0192
223 -10.944739 127.979071 0.0272 -0.2237 0.0293 0.0188
222 -10.994533 127.997716 0.0163 -0.2263 0.0293 0.0185
223 -11.044324 128.016368 0.0217 -0.2273 0.0292 0.0182
224 -11.094114 128.035028 -0.0085 -0.2282 0.0290 0.0178
223 -11.143902 128.053696 0.0448 -0.2298 0.0288 0.0174
224 -11.193688 128.072371 0.0234 -0.2301 0.0287 0.0170
223 -11.243472 128.091055 0.0476 -0.2320 0.0285 0.0166
From that data i must extract the row like this
File 222
222 -10.894943 127.960434 0.0136 -0.2221 0.0292 0.0192
222 -10.994533 127.997716 0.0163 -0.2263 0.0293 0.0185
file 223
223 -10.944739 127.979071 0.0272 -0.2237 0.0293 0.0188
223 -11.044324 128.016368 0.0217 -0.2273 0.0292 0.0182
223 -11.143902 128.053696 0.0448 -0.2298 0.0288 0.0174
I am thinking to use
for i=1:data(:,1)
idx_match=find(data(;,1)==data(;,1)) %matching value in data using Col1
write %wite in new file
end
I am confused to set reference in matching process
  2 件のコメント
Chunru
Chunru 2021 年 8 月 15 日
data = [222 -10.894943 127.960434 0.0136 -0.2221 0.0292 0.0192
223 -10.944739 127.979071 0.0272 -0.2237 0.0293 0.0188
222 -10.994533 127.997716 0.0163 -0.2263 0.0293 0.0185
223 -11.044324 128.016368 0.0217 -0.2273 0.0292 0.0182
224 -11.094114 128.035028 -0.0085 -0.2282 0.0290 0.0178
223 -11.143902 128.053696 0.0448 -0.2298 0.0288 0.0174
224 -11.193688 128.072371 0.0234 -0.2301 0.0287 0.0170
223 -11.243472 128.091055 0.0476 -0.2320 0.0285 0.0166];
ufiles = unique(data(:,1))
ufiles = 3×1
222 223 224
for i=1:length(ufiles)
idx = data(:,1) == ufiles(i);
selected_data = data(idx, :);
% write the data to file
end
Retno Purwaningsih
Retno Purwaningsih 2021 年 8 月 15 日
That's work!!
Thank you for the explanation :D

サインインしてコメントする。

採用された回答

Wan Ji
Wan Ji 2021 年 8 月 15 日
If, for example, your data can be read by readtable function. File name is 'Data.txt', File format is as following:
Col1 Col2 Col3 Col4 Col5 Col6 Co7
222 -10.894943 127.960434 0.0136 -0.2221 0.0292 0.0192
223 -10.944739 127.979071 0.0272 -0.2237 0.0293 0.0188
222 -10.994533 127.997716 0.0163 -0.2263 0.0293 0.0185
223 -11.044324 128.016368 0.0217 -0.2273 0.0292 0.0182
224 -11.094114 128.035028 -0.0085 -0.2282 0.0290 0.0178
223 -11.143902 128.053696 0.0448 -0.2298 0.0288 0.0174
224 -11.193688 128.072371 0.0234 -0.2301 0.0287 0.0170
223 -11.243472 128.091055 0.0476 -0.2320 0.0285 0.0166
Then run the following script:
clc; clear
yourTable = readtable('Data.txt');
column1 = yourTable.Col1;
[column1, idx] = sort(column1);
yourTable = yourTable(idx,:);
[~, ia] = unique(column1);
fileSegment = diff([ia;numel(column1)+1]);
tableCell = mat2cell(yourTable, fileSegment, size(yourTable,2));
celldisp(tableCell) % if file too large,skip this command by using '%'
% write to new files, file name 'table*.txt'
arrayfun(@(i) writetable(tableCell{i},['table',num2str(i),'.txt']),...
1:1:numel(tableCell));
celldisp output will be
tableCell{1} =
Col1 Col2 Col3 Col4 Col5 Col6 Co7
____ __________ __________ ______ _______ ______ ______
222 -10.894943 127.960434 0.0136 -0.2221 0.0292 0.0192
222 -10.994533 127.997716 0.0163 -0.2263 0.0293 0.0185
tableCell{2} =
Col1 Col2 Col3 Col4 Col5 Col6 Co7
____ __________ __________ ______ _______ ______ ______
223 -10.944739 127.979071 0.0272 -0.2237 0.0293 0.0188
223 -11.044324 128.016368 0.0217 -0.2273 0.0292 0.0182
223 -11.143902 128.053696 0.0448 -0.2298 0.0288 0.0174
223 -11.243472 128.091055 0.0476 -0.232 0.0285 0.0166
tableCell{3} =
Col1 Col2 Col3 Col4 Col5 Col6 Co7
____ __________ __________ _______ _______ ______ ______
224 -11.094114 128.035028 -0.0085 -0.2282 0.029 0.0178
224 -11.193688 128.072371 0.0234 -0.2301 0.0287 0.017
Files are generated in the work directory
Also, this way is much faster than comparing them one by one!
  7 件のコメント
Wan Ji
Wan Ji 2021 年 8 月 16 日
arrayfun(@(i) writetable(table(tableCell{i}),['table',num2str(Column1(ia(i))),'.txt'],'Delimiter',' ','WriteVariableNames',0),... 1:1:numel(tableCell)); TRY this command.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by