Brace indexing is not supported for variables of this type.

3 views (last 30 days)
How do I call the sort_nat function to sort folders?
clear all
clc
PF = 'D:\Project\DB1\test\' % Parent Folder
SFL = dir(PF) ;% List the sub folders
[mp1, np] = size(SFL); % compute size = number of subfolders & files & . & ..
csf1=0; % counter of JUST subfolders found in PF
t=1;
for i=3:mp1
%% keep only folders:
P = strfind(SFL(i).name,'.');
if isempty(strfind(SFL(i).name,'.'))
csf1 = csf1 +1; % one sub folder found
SFN = SFL(i).name ;% extract his name
%-----
[SFN,index]= sort_nat(SFN);
c1 = [SFN,index];
%---------------
tifList = ls(sprintf('%s%s%s%s',PF,c1,'\','*.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
Group_Test1(t)=[i-2]
t=t+1;
% PF_SFN_imgName = sprintf('%s%s%s',PF,SFN,'\',tifFileName);
end
end
end
save('Group_Test','Group_Test1');
  2 Comments
per isakson
per isakson on 11 Apr 2021
SFL = dir(PF) ;% List the sub folders
returns folders and files of the folder, PF
per isakson
per isakson on 11 Apr 2021
"Brace indexing is not supported for variables of this type" There is no brace indexing in the code of the question(?).
Please show the full error message.

Sign in to comment.

Answers (1)

per isakson
per isakson on 11 Apr 2021
Edited: per isakson on 11 Apr 2021
"How do I call the sort_nat function to sort folders?"
Examples
Find sorted file names
>> sad = dir('d:\m\cssm\*.txt');
>> cell_array_of_file_names = { sad.name };
>> sorted_cell_array_of_file_names = sort_nat( cell_array_of_file_names );
Find sorted folder names
%%
sad = dir('d:\m\cssm\'); % 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 );
and
%%
list_of_names = {'data_1','data_2','data_3','data_21','data_11','data_12'};
sort_nat( list_of_names )
outputs
ans =
1×6 cell array
Columns 1 through 4
{'data_1'} {'data_2'} {'data_3'} {'data_11'}
Columns 5 through 6
{'data_12'} {'data_21'}
  2 Comments
sun rise
sun rise on 17 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=3: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');
%----------------------
Error using save
Variable 'Group_Test1' not found.
Error in Untitled2 (line 37)
save('Group_Test','Group_Test1');
>>
per isakson
per isakson on 17 Apr 2021
Edited: per isakson on 17 Apr 2021
Matlab has good debugging features, see Debug a MATLAB Program.
The value of sorted_cell_array_of_folder_names is a row vector (the expression {sad([sad.isdir]).name}; returns a row). Thus, the statement
[mp1, np] = size(sorted_cell_array_of_folder_names);
assigns the value 1 to mp1 and consequently the statements of the for-loop (for i=3:mp1) are never executed. That explains why Group_Test1 is not defined at the end of your script.

Sign in to comment.

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!