How can I remove . and ..
2 次查看(过去 30 天)
显示 更早的评论
clear all
clc
sad = dir('D:\Project\DB1\test\'); % Returns both folders and files
% sad = dir(pwd); % Returns both folders and files
cell_array_of_folder_names = {sad([sad.isdir]).name}; % Select folder names
% cell_array_of_folder_names( strncmp( cell_array_of_folder_names, ".", 1 ) ) = []; % Remove '.' and '..'
sorted_cell_array_of_folder_names = sort_nat( cell_array_of_folder_names );
% sorted_cell_array_of_folder_names = cell_array_of_folder_names; % if you don't have sort_nat
whos sorted_cell_array_of_folder_names
%----------------
[mp1, np] = size(sorted_cell_array_of_folder_names); % compute size = number of subfolders & files & . & ..
csf1=0; % counter of JUST subfolders found in PF
t=1;
Group_Test1 = zeros(1, mp1);
for i=1:mp1
% Skip . and ..
if endsWith(sorted_cell_array_of_folder_names{i}, '.') || endsWith(sorted_cell_array_of_folder_names{i}, '..')
% Skip . and ..
continue; % Skip to bottom of loop.
end
% Keep only folders:
csf1 = csf1 +1; % one sub folder found
SFN = sorted_cell_array_of_folder_names(i).name ;% extract his name
tifList = ls(sprintf('%s%s%s%s',PF,SFN,'\','*.tif')); % list all jpg files
[ms1, ns] = size(tifList); % ms = number of image files found
% Processing for each tif file:
for j=1:ms1
tifFileName = tifList(j,:); % extract name of tif file
% IM=imread([PF SFN '\' tifFileName]);
%t=1;
%for i=1:csf1
% for j=1:ms1
Group_Test1(t)={i};
t=t+1;
end
PF_SFN_imgName = sprintf('%s%s%s',PF,SFN,'\',tifFileName);
end
Group_Test1 = Group_Test1(1:t-1); % Crop off any unused elements
save('Group_Test','Group_Test1');
%----------------------
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/634020/image.png)
1 个评论
Stephen23
2021-5-28
Tip: use FULLFILE to join strings into one filepath, rather than SPRINTF or string concatenation.
采纳的回答
Stephen23
2021-5-28
Replace
{sad([sad.isdir]).name}
with
setdiff({sad([sad.isdir]).name},{'..','.'})
17 个评论
Walter Roberson
2021-5-28
if ispc()
PF = fullfile('D:\Project\DB1\test\', '*', '*.tif');
else
%for demonstration purposes ONLY.
%MATLAB Answers does not have nat_sort installed, so install it
td = tempname;
mkdir(td);
tz = [td '.zip'];
urlwrite('https://www.mathworks.com/matlabcentral/mlc-downloads/downloads/submissions/10959/versions/4/download/zip/sort_nat.zip', tz);
unzip(tz, td)
addpath(td)
%now point us to a directory we know that some tif files are in
PF = fullfile( fileparts(fileparts(which('cameraman.tif'))), '**', '*.tif');
end
sad = dir(PF); % Returns files only
cell_array_of_file_names = fullfile({sad.folder}, {sad.name});
[folders, basenames, ext] = fileparts(cell_array_of_file_names);
G = findgroups(folders);
[~, labels, ~] = fileparts(folders);
Groups = struct();
maxG = max(G);
for fidx = 1 : maxG
matches = find(G == fidx);
these_entries = cell_array_of_file_names(matches);
these_entries = sort_nat( these_entries );
this_label = labels{matches(1)};
%these_entries is a cell array containing only file names
%that are all part of the same folder, and the folder name
%is in this_label
Groups.(this_label) = these_entries;
end
Groups
Groups.(this_label).'
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!