How do I iterate through many .txt files and ignore part of the name?

6 visualizaciones (últimos 30 días)
So I am trying to analyze data that is saved and stored in many .txt files. The .txt files are indexed by one part of the name (one variable, basically), changing in a linear way, so I just loop through them. There is one issue: as each file gets generated, it gets saved with a timestamp down to the second (I cannot change this) as the first 13 characters in the name. Is there a way to tell Matlab that these 13 characters can be anything but that they don't matter? I am on OS X if that helps. Basically, the code is as shown below.
In filename, the first 13 characters (12 digits and the space) I have to manually delete from the file name in order to analyze this data; it is incredibly annoying. The actual, as-saved file name is, for example, '201001150802 File_name_here_blah_blah_B200F250S250O7000D800P-4y.txt' and the number between B and F I iterate through. Is there a way to ignore or allow any character for the first thirteen characters?
path = ['/Users/me/Dropbox/Company/Data/Sample_Data_Folder/'];
B = 200:10:1900;
for j = 1:length(B)
filename = ['File_name_here_blah_blah_B' sprintf('%d',B(j)) 'F250S250O7000D800P-4y.txt'];
filestring = strcat(path,filename);
array = textread(filestring); %Use textread for .txt files
x_data = array(:,1); %I use these to plot.
y_data = array(:,2);
end

Respuesta aceptada

Mario Malic
Mario Malic el 1 de Oct. de 2020
Editada: Mario Malic el 1 de Oct. de 2020
You can use dir function.
Dir_files = dir('*4y.*txt'); % wildcard, add as many chars as necessary to get needed files
List_Files = {Dir_files.name}'; % Cell (length(Dir_files), 1) that contain filenames to read
  3 comentarios
Blenndrman
Blenndrman el 2 de Oct. de 2020
Thanks for taking the time. I will say, I am still confused on implementation. Would my code look something like this?
Dir_files = dir('*blah_blah*.txt'); %To get files with blah_blah in them
List_Files = {Dir_files.name}'; %Store all the names here
And then access and iterate through the structure in the loop? Because that is not working for me. I am thinking Matlab does not know how to understand that dir should be looking in the folder specified in path. I do not know how to tell dir which folder to look in.
Rik
Rik el 2 de Oct. de 2020
How are you creating the folder name? This should work as expected:
dir(fullfile(path,'*y4*'))
Note that your variable path is shadowing the built-in function.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by