How to import a sequence of file .txt
조회 수: 2 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!