extract values and save them as unique txt files

2 vues (au cours des 30 derniers jours)
sensation
sensation le 27 Juin 2018
Commenté : sensation le 27 Juin 2018
Hi, I have a vector and what to extract the unique values of the first column and save those as separate txt file with the same ID as unique ID. Thanks a lot! For example A:
A dataset:
21 723204 12
21 723204 10.5000000000000
21 723204 13.5000000000000
21 723292 20
21 723356 21
21 723447 19
21 723483 17
21 723503 16
21 723503 11
22 723572 11
22 723594 12.5000000000000
22 723621 16.5000000000000
22 723663 16
23 723692 19
23 723720 26
23 723720 32
23 723783 22
%end
and result should be: 21.txt, 22.txt, 23.txt with e.g. 23.txt is:
723692 19
723720 26
723748 32
723783 22
and so on...(so without first column in the resulting txt file).
so far I did :
[C,ia,ic] = unique(A(:,1),'rows')
rows21 = A(C==21, :); % but I do not want to this separately since it takes a lot of time. I would like to do with for loop if possible so I create automatically txt files? thanks a lot!

Réponse acceptée

Image Analyst
Image Analyst le 27 Juin 2018
This works:
data = dlmread('text.txt')
% Get unique values in column 1
uniqueValues = unique(data(:, 1))'
for k = uniqueValues
fullFileName = fullfile(pwd, sprintf('%d.txt', k))
rowsToWrite = data(:, 1) == k;
dlmwrite(fullFileName, data(rowsToWrite, 2:end), 'delimiter', ' ', 'precision','%g');
end

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by