pick files within dir
显示 更早的评论
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 个评论
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
采纳的回答
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 个评论
so due to my complete code using in other areas
files = dir(test{2});
i need to add the add ons here, after ^^^
match being the function to modify to pick between the post files
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);
would your code do that for me ??
it worked but it picked other post files that i did not realized where there and did not want. how may i pick just the comp post files ?
I do not know what you mean by "comp post files" ?
I do not know what "readnames()" is.
I will assume that test{2} is the name of a folder.
I am not clear whether you are looking for 'Post' or 'post' in the name ?
dinfo = dir(fullfile(test{2}, '*'));
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});
would you explain this line - what it does
ndpinfo([ndpinfo.isdir]) = []; %remove . and .. and any folder names
i guess what i am saying is that theres two conditions that the dir has to read through "comp" and "post"
the files names are "alwentrtginrig_comp_kaejtnkjbawekjfgber_post"
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.
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});
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 File Operations 的更多信息
标签
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
