How to import a sequence of file .txt

3 ビュー (過去 30 日間)
Marco Pillon
Marco Pillon 2021 年 4 月 24 日
コメント済み: Mathieu NOE 2021 年 5 月 17 日
Hello,
I am a recent matlab user who cannot understand how to import a large series of .txt files in sequence (each one is made up of a series of numbers).
I try to explain myself:
1) I should create a script that reads and imports in sequence and as vectors the .txt files contained in a certain folder (one file = one vector). The files are of the type
waveform_1_1.txt, waveform_1_2.txt up to _16_16 etc;
2) from time to time I should elaborate the vector with some function, "so as to obtain a certain number" to be associated with an element of a certain matrix. So the
elaboration of the waveform_1_1.txt I should associate it to the element 1,1, the elaboration of the waveform_1_2.txt I should associate it to the element 1,2 and so on.
Would anyone know how to do it?
Would anyone know how to help me complete the for loop in areas with "green % ......"
thank you
Marco
gdm = ones(16); % matrix that I have to fill
for i=1:256 % 256 is number of file.txt
%..........
%..........
%..........
Vector_mean_i = mean(%.......) % now I have to average vector i
%..........
%.......... %I have to insert the processing of the file, that is Vector_mean_i, in the first element of the gdm array and so on with the for loop
%..........
end

採用された回答

Mathieu NOE
Mathieu NOE 2021 年 5 月 10 日
hello Marco
see below an example how to read all txt files inside a folder and do the batch processing - like here I simply do the averaging of the data and store it in an array
I don't know exactly how you wanted to use the first and second numrical indexes inside the data filename , but the code do extract them, see how you want to use that info
also note that this code does not follow the "natural" order of the filenames , so if that is important, we can use that very handy function natsortfiles available on File Exchange : Natural-Order Filename Sort - File Exchange - MATLAB Central (mathworks.com)
this is my demo code so far :
clc
clearvars
% reading of data txt files
% Folder = 'C:\Your\Folder';
Folder = cd;
FileList = dir (fullfile(Folder, 'waveform_*.txt'));
for iFile = 1:numel(FileList)
FileName = FileList(iFile).name;
FileNamePath = fullfile(Folder, FileName);
ind = findstr(FileName,'_');
indp = findstr(FileName,'.txt');
index1(iFile) = str2num(FileName(ind(1)+1:ind(2)-1)); % this is the first number in the filename
index2(iFile) = str2num(FileName(ind(2)+1:indp-1)); % this is the second number in the filename
% load the data
data = importdata(FileNamePath);
% compute the mean (example)
Vector_mean_i(iFile) = mean(data);
end
  9 件のコメント
Stephen23
Stephen23 2021 年 5 月 17 日
編集済み: Stephen23 2021 年 5 月 17 日
"sure, that's what I already suggested in this post (my answer 10 May 2021 at 13:44) - see above "
In that post you sorted just the filenames (as a cell array created from a comma-separated list):
FileList_sorted = natsortfiles({FileList.name});
I recommend simply sorting the entire structure, not just a cell array of filenames:
FileList = natsortfiles(FileList);
Mathieu NOE
Mathieu NOE 2021 年 5 月 17 日
OK tx for the tip - will try to remember this for next time ...

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by