Filter löschen
Filter löschen

Dot indexing is not supported for variables of this type.

6 Ansichten (letzte 30 Tage)
sun rise
sun rise am 21 Apr. 2021
Beantwortet: Image Analyst am 22 Apr. 2021
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,'.');
>>

Akzeptierte Antwort

Jan
Jan am 21 Apr. 2021
Bearbeitet: Jan am 21 Apr. 2021
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 Kommentare
sun rise
sun rise am 22 Apr. 2021
tifList must be ordered using the sort_nat function
Jan
Jan am 22 Apr. 2021
Then replace
FolderName = {tifList.folder};
by
FolderName = sort_nat({tifList.folder});

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 22 Apr. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by