pick files within dir

8 vues (au cours des 30 derniers jours)
Ihaveaquest
Ihaveaquest le 14 Sep 2022
Commenté : Ihaveaquest le 15 Sep 2022
I have a list create by dir
i want to pick specific files in the dir the name differentiators are genreal name with "post" on the title and without
I want to pick "post" files to create new table with the
files = dir(test{2});
match = readnames(files.name,'Post'); %I know this is wrong but im not sure hwat else to try
comp.files = match;
x = size(comp.files);
help please
  1 commentaire
Ihaveaquest
Ihaveaquest le 15 Sep 2022
it all looks great thak you i figured it out
files = dir(fullfile('*Comp*Post*'));
this si what i was looking for .. thank sorry for the misunderstanding

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 14 Sep 2022
dinfo = dir('*post*');
comp.files = fullfile({dinfo.folder}, {dinfo.name});
numfiles = length(comp.files);
Now comp.files will be a cell array of character vectors, each of which is a fully-qualified file name that has "post" as part of the name.
For other files without "post" the code is slightly longer
npdinfo = dir('*');
ndpinfo([ndpinfo.isdir]) = []; %remove . and .. and any folder names
npdinfo(contains({npdinfo.name}, 'post')) = []; %remove names containing 'post'
comp.npfiles = fullfile({npdinfo.folder}, {npdinfo.name});
numnpfiles = length(comp.npfiles);
... If you are wanting both lists, then it is easier to just do one pass, like
dinfo = dir('*');
dinfo([dinfo.isdir]) = []; %remove . and .. and any folder names
mask = contains({dinfo.name}, 'post');
comp.files = fullfile({dinfo(mask).folder}, {dinfo(mask).name});
numfiles = length(comp.files);
comp.npfiles = fullfile({dinfo(~mask).folder}, {dinfo(~mask).name});
  7 commentaires
Walter Roberson
Walter Roberson le 14 Sep 2022
The output from dir() is a struct. For your purposes, the important fields are 'isdir' (which is true if the directory entry is for a folder); and 'folder' (name of the containing folder); and 'name' (name of the file within the folder).
For any nonscalar structure, STRUCTURENAME.FIELDNAME undergoes "struct expansion". The result is the same as if you had coded
STRUCTURENAME(1).FIELDNAME, STRUCTURENAME(2).FIELDNAME, STRUCTURENAME(3).FIELDNAME, etc
-- as if you had coded all of that, commas included, as separate parameters to whatever function you are using. So if you use the [] function
[STRUCTURENAME.FIELDNAME]
the result is as-if you had typed
[STRUCTURENAME(1).FIELDNAME, STRUCTURENAME(2).FIELDNAME, STRUCTURENAME(3).FIELDNAME, etc]
which would create a list containing all of the FIELDNAME files for each entry in the non-scalar structure STRUCTURENAME
So [dinfo.isdir] creates a vector of values, the first of which is dinfo(1).isdir, the second is dinfo(2).isdir, and so on -- so [dinfo.isdir] is a vector that tells you whether each of the corresponding dinfo entries refers to a folder.
As this would be a logical vector, it can be used to select elements of an array. dinfo([dinfo.isdir]) is therefore subset of the dinfo struct such that the isdir entry was true for that entry.
Then when you assign [] to those entries, that is MATLAB syntax to delete the entries.
So after
ndpinfo([ndpinfo.isdir]) = [];
then ndpinfo will not contain any entries that deal with folders.
The further information that is relevant is that unless you are using a quite old filesystem type, then when you ask for a list of all entries in a folder, using '*' as the pattern, then you will not just be given the list of files. Instead, you will be given the list of files (and subfolders) and one entry named . and one entry named .. . The entry . refers to the directory itself, and the entry .. refers to the parent directory. These turn out to be quite handy to use for various purposes, but they are not the names of files you happen to be looking for in your particular purpose, so you want to remove those from the list.
It is common but mistaken to believe that the . and .. entries will always be the first two entries returned by dir(), so it is not unusual for people to take dir() results and loop starting with entry #3. That is incorrect code in every operating system that MATLAB has ever been supported on. If you have a reason to avoid all directory entries including . and .. then use the isdir filtering that I show; if you just want to avoid . and .. specifically then filter for those based on name, not on assumptions about their relative positions.
Walter Roberson
Walter Roberson le 14 Sep 2022
dinfo = dir(fullfile(test{2}, '*'));
dinfo([dinfo.isdir]) = []; %remove . and .. and any folder names
mask = contains({dinfo.name}, 'post') & contains({dinfo.name}, 'comp');
comp.files = fullfile({dinfo(mask).folder}, {dinfo(mask).name});
numfiles = length(comp.files);
comp.npfiles = fullfile({dinfo(~mask).folder}, {dinfo(~mask).name});

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

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

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by