How to import a sequence of file .txt

2 visualizaciones (últimos 30 días)
Marco Pillon
Marco Pillon el 24 de Abr. de 2021
Comentada: Mathieu NOE el 17 de Mayo de 2021
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

Respuesta aceptada

Mathieu NOE
Mathieu NOE el 10 de Mayo de 2021
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 comentarios
Stephen23
Stephen23 el 17 de Mayo de 2021
Editada: Stephen23 el 17 de Mayo de 2021
"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 el 17 de Mayo de 2021
OK tx for the tip - will try to remember this for next time ...

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by