Find specific files based on sections of file name

209 vues (au cours des 30 derniers jours)
Robert
Robert le 12 Jan 2018
Commenté : Walter Roberson le 19 Juil 2019
Hello,
I want to generate an analysis using .csv files. How could I make a search to load only files that have specifics sections in the file name? Like this example, assuming folder AA has files:
Lab_001_CAN_033.csv
Lab_011_USA_031.csv
Lab_021_CAN_031.csv
Lab_091_CAN_032.csv
Lab_001_CAN_003.csv
Lab_011_USA_011.csv
Lab_021_CAN_001.csv
Lab_091_CAN_002.csv
But just read files with the section 'CAN' and the condition that the last 3 numbers on file name (sample number) have to be between 0 and 10 (or 000 and 010), so I can only download the files:
Lab_001_CAN_003.csv
Lab_021_CAN_001.csv
Lab_091_CAN_002.csv
For a few files this is not a problem, but for thousands of files (as I have) it gets complicated.
Thanks!
  1 commentaire
Robert
Robert le 12 Jan 2018
Sorry, I forgot to add that I also would like to make copies of those files and save them to a folder named BB.

Connectez-vous pour commenter.

Réponse acceptée

Kris Fedorenko
Kris Fedorenko le 12 Jan 2018
Function dir with a wildcard (*) might be somewhat helpful. For example
fstruct = dir('*_CAN_*.csv')
would return a structure of all files with a .CSV extension containing "_CAN_" as part of their names.
For only getting the numbers between 001 and 010 you would need to do something more involved. You might consider an if-statement to filter the results of the "dir" command while looping over them. Or maybe you can take a look at Regular Expressions.
Note that
fstruct = dir('*_CAN_00*.csv')
would already return all files containing "_CAN_00" in their name, so one possible ad-hoc workaround would be to do that to get files with _CAN_001, _CAN_002, ..., _CAN_009, and run a separate query for _CAN_010 file
  3 commentaires
Coco
Coco le 19 Juil 2019
Hi Kris,
I have the save question. After i get the struct which have all files, whats the code of next step if i want to save those files into a new folder? I am a nebby of Matlab. Thank you.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by