How can I remove . and ..
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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');
%----------------------

댓글 수: 1
Stephen23
2021년 5월 28일
Tip: use FULLFILE to join strings into one filepath, rather than SPRINTF or string concatenation.
채택된 답변
Stephen23
2021년 5월 28일
0 개 추천
Replace
{sad([sad.isdir]).name}
with
setdiff({sad([sad.isdir]).name},{'..','.'})
댓글 수: 17
Walter Roberson
2021년 5월 28일
Note that in theory using setdiff() like that could reorder the files. In practice it is unlikely to reorder the files (not impossible though.)
Stephen23
2021년 5월 28일
Given that the order returned by DIR is not specified, I don't see how that makes much difference.
sun rise
2021년 5월 28일
Name Size Bytes Class Attributes
sorted_cell_array_of_folder_names 1x937 110350 cell
Dot indexing is not supported for variables of this type.
Error in Untitled (line 24)
SFN = sorted_cell_array_of_folder_names(i).name ;% extract his name
>>
sun rise
2021년 5월 28일
This error after changes
Walter Roberson
2021년 5월 28일
Replace
SFN = sorted_cell_array_of_folder_names(i).name ;% extract his name
with
SFN = sorted_cell_array_of_folder_names{i} ;% extract his name
Walter Roberson
2021년 5월 28일
In context the hypothetical reordering of setdiff() is irrelevant anyhow because the nat sort is done right afterwards ;-)
sun rise
2021년 5월 28일
It does not enter the for j loop, so Group_Test1 is empty
sun rise
2021년 5월 28일

Walter Roberson
2021년 5월 28일
Do you still have
if endsWith(sorted_cell_array_of_folder_names{i}, '.') || endsWith(sorted_cell_array_of_folder_names{i}, '..')
if so then you can remove that test and the continue, since you already filtered out . and ..
sun rise
2021년 5월 28일

Walter Roberson
2021년 5월 28일
I recommend that you rewrite
tifList = ls(sprintf('%s%s%s%s',PF,SFN,'\','*.tif')); % list all jpg files
in terms of dir() and fullfile()
I do not see where you have defined PF ?
Is there a reason you are not using
PF = 'D:\Project\DB1\test';
sad = dir(fullfile(PF, '*', '*.tif'));
cell_array_of_file_names = fullfile({sad.folder}, {sad.name});
and then cell_array_of_file_names would be the complete list of all .tif files that are one folder level down from DB1\test ?
sun rise
2021년 5월 28일
I want to order each folder separately and give all the pictures in it the same name or label
sun rise
2021년 5월 28일
First the folders are arranged, then all the pictures inside the folder are named with the same name. For example: The pictures in Volume 1 are all named with Name 1 ... and so on ...
Walter Roberson
2021년 5월 28일
[folders, basenames, ext] = fileparts(cell_array_of_file_names);
G = findgroups(folders);
[~, labels, ~] = fileparts(folders);
Now you for each entry cell_array_of_file_names{K} then its associated label is labels{K} and you can process all members of the same folder by processing
maxG = max(G);
for fidx = 1 : maxG
matches = find(G == fidx);
these_entries = cell_array_of_file_names(matches);
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
end
sun rise
2021년 5월 28일
clear all
clc
sad = dir('D:\Project\DB1\test\'); % 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 '..'
folder_names = sort_nat( cell_array_of_folder_names );
%----------------
[folders, basenames, ext] = fileparts(cell_array_of_folder_names);
G = findgroups(folders);
[~, labels, ~] = fileparts(folders);
maxG = max(G);
for fidx = 1 : maxG
matches = find(G == fidx);
these_entries = folder_names(matches);
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
end
%save('Group_Test','Group_Test1');
%----------------------
Error using fileparts (line 37)
Input must be a row vector of characters or string scalar.
Error in Untitled3 (line 9)
[folders, basenames, ext] = fileparts(cell_array_of_folder_names);
>>
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 = struct with fields:
imdata: {1×33 cell}
Groups.(this_label).'
ans = 33×1 cell array
{'/MATLAB/toolbox/images/imdata/AT3_1m4_01.tif'}
{'/MATLAB/toolbox/images/imdata/AT3_1m4_02.tif'}
{'/MATLAB/toolbox/images/imdata/AT3_1m4_03.tif'}
{'/MATLAB/toolbox/images/imdata/AT3_1m4_04.tif'}
{'/MATLAB/toolbox/images/imdata/AT3_1m4_05.tif'}
{'/MATLAB/toolbox/images/imdata/AT3_1m4_06.tif'}
{'/MATLAB/toolbox/images/imdata/AT3_1m4_07.tif'}
{'/MATLAB/toolbox/images/imdata/AT3_1m4_08.tif'}
{'/MATLAB/toolbox/images/imdata/AT3_1m4_09.tif'}
{'/MATLAB/toolbox/images/imdata/AT3_1m4_10.tif'}
{'/MATLAB/toolbox/images/imdata/autumn.tif' }
{'/MATLAB/toolbox/images/imdata/board.tif' }
{'/MATLAB/toolbox/images/imdata/cameraman.tif' }
{'/MATLAB/toolbox/images/imdata/canoe.tif' }
{'/MATLAB/toolbox/images/imdata/cell.tif' }
{'/MATLAB/toolbox/images/imdata/circbw.tif' }
{'/MATLAB/toolbox/images/imdata/circuit.tif' }
{'/MATLAB/toolbox/images/imdata/eight.tif' }
{'/MATLAB/toolbox/images/imdata/forest.tif' }
{'/MATLAB/toolbox/images/imdata/hotcoffee.tif' }
{'/MATLAB/toolbox/images/imdata/kids.tif' }
{'/MATLAB/toolbox/images/imdata/logo.tif' }
{'/MATLAB/toolbox/images/imdata/m83.tif' }
{'/MATLAB/toolbox/images/imdata/mandi.tif' }
{'/MATLAB/toolbox/images/imdata/moon.tif' }
{'/MATLAB/toolbox/images/imdata/mri.tif' }
{'/MATLAB/toolbox/images/imdata/paper1.tif' }
{'/MATLAB/toolbox/images/imdata/pout.tif' }
{'/MATLAB/toolbox/images/imdata/shadow.tif' }
{'/MATLAB/toolbox/images/imdata/spine.tif' }
sun rise
2021년 5월 31일
>> Untitled3
Error using fileparts (line 37)
Input must be a row vector of characters or string scalar.
Error in Untitled3 (line 21)
[folders, basenames, ext] = fileparts(cell_array_of_file_names);
>>
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
