Dot indexing is not supported for variables of this type.

6 次查看(过去 30 天)
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 '..'
sorted_cell_array_of_folder_names = sort_nat( 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;
for i=1:mp1
%% keep only folders:
P = strfind(sorted_cell_array_of_folder_names(i).name,'.');
if isempty(strfind(sorted_cell_array_of_folder_names(i).name,'.'))
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-2};
t=t+1;
end
end
PF_SFN_imgName = sprintf('%s%s%s',PF,SFN,'\',tifFileName);
end
save('Group_Test','Group_Test1');
%----------------------
Dot indexing is not supported for variables of this type.
Error in Untitled2 (line 15)
P = strfind(sorted_cell_array_of_folder_names(i).name,'.');
>>

采纳的回答

Jan
Jan 2021-4-21
编辑:Jan 2021-4-21
Replace:
strfind(sorted_cell_array_of_folder_names(i).name,'.');
% by
strfind(sorted_cell_array_of_folder_names{i},'.');
The name of the variable is cruel: "sorted_cell_array_of_folder_names". But it implies, that the variable is a cell array. So getting the field "name" is not working.
Your code can be simplified:
  • Omit the brute "clear all". It removes all loaded functions from the memory. The reloading from the slow disk wastes time without any benefit.
  • "folder_names" is a nice name, while "cell_array_of_folder_names" impedes reading the code.
  • contains and startsWith are useful. Compare:
cell_array_of_folder_names( strncmp( cell_array_of_folder_names, ".", 1 ) ) = [];
with
folder_names(startsWith(folder_names, ".")) = [];
and
P = strfind(sorted_cell_array_of_folder_names(i).name,'.');
if isempty(strfind(sorted_cell_array_of_folder_names(i).name,'.'))
with
P = contains(folder_names{i}, '.');
if ~P
  • This is cruel:
tifList = ls(sprintf('%s%s%s%s',PF,SFN,'\','*.tif')); % list all jpg files
[ms1, ns] = size(tifList);
Creating a char output only to count the lines is very indirect. Nicer:
tifList = dir(fullfile(PF, SFN, '*.tif'));
ms1 = numel(tifList);
  • fullfile cares for the proper file separators.
  4 个评论
Jan
Jan 2021-4-22
Then replace
FolderName = {tifList.folder};
by
FolderName = sort_nat({tifList.folder});

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2021-4-22

Community Treasure Hunt

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

Start Hunting!

Translated by