How to import a sequence of file .txt

조회 수: 2 (최근 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 CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by