How can i edit data in multiple text files?

I have hundreds of .txt files of data, and i need to order that data, and save only the top and bottom 10% of it to the same .txt file.
I've looked arround and tryed some things out but with no luck so far.
This is what i have.
clear all
close all
clc
Folder = 'C:\Users\Bruno\Desktop\teste123';
FilePattern = fullfile(Folder, '*.txt');
TheFiles = dir(FilePattern);
for k = 1 : length(TheFiles)
BaseFileName = TheFiles(k).name;
FullFileName = fullfile(TheFiles(k).folder, BaseFileName);
Data = readmatrix(BaseFileName);
SortedData = sortrows (Data,-5);
RowsToDelete = [11:83];
SortedData(RowsToDelete,:) = [];
HighCorr = SortedData(1:10,:);
LowCorr = SortedData(11:20,:);
end
With this script, matlab only gives me the top and bottom 10% (named HighCorr and LowCorr) of the first file.
I need to save this in the respective .txt file, and do the same with the rest of my folder.
Can someone help please?

11 件のコメント

Jorg Woehl
Jorg Woehl 2021 年 3 月 24 日
Hi Bruno, I noticed that you use BaseFileName instead of FullFileName in the call to readmatrix.
Otherwise, would it be possible to provide some sample files for testing?
Bruno Carvalho
Bruno Carvalho 2021 年 3 月 24 日
Thanks for taking the time to help me.
I'm using these 5 .txt files as a test drive.
Also, i switched to FullFileName but there were no visible changes in the output
Jan
Jan 2021 年 3 月 24 日
What does "with no luck so far" mean? We see the code. But what is the problem?
Bruno Carvalho
Bruno Carvalho 2021 年 3 月 24 日
It means i dont know how to do what is asked of me.
I'm looking arround trying to find answers, on what to write on the code to basicly edit the .txt files in matlab.
I need to get the data from the .txt, order it, from the bigger to smaller numbers, and delete everything thats not the top and bottom 10%. I can do that with 1 .txt file, but im trying to learn how to do it on multiple files. "With no luck so far"
Jan
Jan 2021 年 3 月 24 日
The code you have posted does this on all files inside a folder already. Therefore I do not understand, what you are asking for. Currently the code does not do anything with the results, but overwrites them in the next iteration. Is this your problem?
Bruno Carvalho
Bruno Carvalho 2021 年 3 月 24 日
Yes, i think so. Do you have any sugestion?
Jan
Jan 2021 年 3 月 24 日
Suggestion for what? Do you want to collect the data in an array, or to modify the files?
Bruno Carvalho
Bruno Carvalho 2021 年 3 月 24 日
I want to modify the files
Jan
Jan 2021 年 3 月 24 日
What about:
SortedData = sortrows (Data, -5);
n = size(SortedData, 1);
Data = SortedData([1:10, 84:s], :);
writematrix(FullFileName, Data);
Bruno Carvalho
Bruno Carvalho 2021 年 3 月 24 日
Yes, thats perfect. Thank you very much.
Jan
Jan 2021 年 3 月 24 日
Then I will repost it as an answer.

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

 採用された回答

Jan
Jan 2021 年 3 月 24 日

1 投票

SortedData = sortrows (Data, -5);
n = size(SortedData, 1);
Data = SortedData([1:10, 84:s], :);
writematrix(FullFileName, Data);

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeVariables についてさらに検索

タグ

質問済み:

2021 年 3 月 23 日

コメント済み:

Jan
2021 年 3 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by