For Loop doesn't execute for some specific folders.

2 views (last 30 days)
Hi,
I have written a function which reads sub folders with in the parent directory and populates tables, aligns them and then plots the stacked plot of selected paramters.
function processParentFolder(parent_folder_path)
% Step 1: Get the subfolders within the parent folder
subfolders = dir(parent_folder_path);
subfolders = subfolders([subfolders.isdir]); % Only directories
subfolders = subfolders(~ismember({subfolders.name}, {'.', '..'})); % Exclude '.' and '..'
% Check if there are subfolders
if isempty(subfolders)
% Step 1: No subfolders, get locations of CSV files
[elec_data, raw_mag, beci_data, patch_imp_raw, patch_imp_fil, cath_imp_raw, cath_imp_fil, comp_mag, fil_mag] = findDataFiles(parent_folder_path);
% Process elec_data if it's non-empty
if ~isempty(elec_data)
elec_data = readElecLocations(elec_data);
disp('Processing elec_data...');
% Further processing of elec_data (Implement this part later)
else
disp('No files found for elec_data.');
end
% Process raw_mag if it's non-empty
if ~isempty(raw_mag)
raw_mag = read_raw_magnetic_data(raw_mag);
disp('Processing raw_mag...');
% Further processing of raw_mag (Implement this part later)
else
disp('No files found for raw_mag.');
end
% Process beci_data if it's non-empty
if ~isempty(beci_data)
beci_data = becidata(beci_data);
disp('Processing beci_data...');
% Further processing of beci_data (Implement this part later)
else
disp('No files found for beci_data.');
end
% Process patch_imp_raw if it's non-empty
if ~isempty(patch_imp_raw)
patch_imp_raw = readpatchdata(patch_imp_raw);
disp('Processing patch_imp_raw...');
% Further processing of patch_imp_raw (Implement this part later)
else
disp('No files found for patch_imp_raw.');
end
% Process patch_imp_fil if it's non-empty
if ~isempty(patch_imp_fil)
patch_imp_fil = readpatchdata(patch_imp_fil);
disp('Processing patch_imp_fil...');
% Further processing of patch_imp_fil (Implement this part later)
else
disp('No files found for patch_imp_fil.');
end
% Process cath_imp_raw if it's non-empty
if ~isempty(cath_imp_raw)
cath_imp_raw = readcathloc(cath_imp_raw);
disp('Processing cath_imp_raw...');
% Further processing of cath_imp_raw (Implement this part later)
else
disp('No files found for cath_imp_raw.');
end
% Process cath_imp_fil if it's non-empty
if ~isempty(cath_imp_fil)
cath_imp_fil = readcathloc(cath_imp_fil);
disp('Processing cath_imp_fil...');
% Further processing of cath_imp_fil (Implement this part later)
else
disp('No files found for cath_imp_fil.');
end
% Process comp_mag if it's non-empty
if ~isempty(comp_mag)
comp_mag = read_computed_mag(comp_mag);
disp('Processing comp_mag...');
% Further processing of comp_mag (Implement this part later)
else
disp('No files found for comp_mag.');
end
% Process fil_mag if it's non-empty
if ~isempty(fil_mag)
fil_mag = read_filtered_mag(fil_mag);
disp('Processing fil_mag...');
% Further processing of fil_mag (Implement this part later)
else
disp('No files found for fil_mag.');
end
% Step 3: Align tables
non_empty_tables = {elec_data, raw_mag, patch_imp_raw, patch_imp_fil, cath_imp_raw, cath_imp_fil, comp_mag, fil_mag};
non_empty_tables = non_empty_tables(~cellfun(@isempty, non_empty_tables));
alignTables(non_empty_tables{:});
% Step 4: Calculate beci_status_bit
if ~isempty(elec_data) && ~isempty(beci_data)
beci_status_bit = bits(elec_data, beci_data);
disp('Calculated beci_status_bit.');
disp(height(elec_data))
disp(height(raw_mag))
disp(height(beci_status_bit))
% Further processing of beci_status_bit (Implement this part later)
else
disp('No elec_data or beci_data found. Cannot calculate beci_status_bit.');
end
% Step 5: Concatenate voxel plot data and plot
voxel_plot_data = [raw_mag(:,["tx_51_raw","ty_51_raw","tz_51_raw"]),elec_data(:,["c52z","c52x","c52y"]),beci_status_bit(:,["beci_status_bit"])];
figure()
stackedplot(voxel_plot_data)
else
% Step 2: Process each subfolder
for i = 1:numel(subfolders)
subfolder_path = fullfile(parent_folder_path, subfolders(i).name);
[elec_data, raw_mag, beci_data, patch_imp_raw, patch_imp_fil, cath_imp_raw, cath_imp_fil, comp_mag, fil_mag] = findDataFiles(subfolder_path);
% Process elec_data if it's non-empty
if ~isempty(elec_data)
elec_data = readElecLocations(elec_data);
disp(['Processing elec_data in subfolder ', subfolders(i).name, '...']);
% Further processing of elec_data (Implement this part later)
else
disp(['No files found for elec_data in subfolder ', subfolders(i).name, '.']);
end
% Process raw_mag if it's non-empty
if ~isempty(raw_mag)
raw_mag = read_raw_magnetic_data(raw_mag);
disp(['Processing raw_mag in subfolder ', subfolders(i).name, '...']);
% Further processing of raw_mag (Implement this part later)
else
disp(['No files found for raw_mag in subfolder ', subfolders(i).name, '.']);
end
% Process beci_data if it's non-empty
if ~isempty(beci_data)
beci_data = becidata(beci_data);
disp(['Processing beci_data in subfolder ', subfolders(i).name, '...']);
% Further processing of beci_data (Implement this part later)
else
disp(['No files found for beci_data in subfolder ', subfolders(i).name, '.']);
end
% Process patch_imp_raw if it's non-empty
if ~isempty(patch_imp_raw)
patch_imp_raw = readpatchdata(patch_imp_raw);
disp(['Processing patch_imp_raw in subfolder ', subfolders(i).name, '...']);
% Further processing of patch_imp_raw (Implement this part later)
else
disp(['No files found for patch_imp_raw in subfolder ', subfolders(i).name, '.']);
end
% Process patch_imp_fil if it's non-empty
if ~isempty(patch_imp_fil)
patch_imp_fil = readpatchdata(patch_imp_fil);
disp(['Processing patch_imp_fil in subfolder ', subfolders(i).name, '...']);
% Further processing of patch_imp_fil (Implement this part later)
else
disp(['No files found for patch_imp_fil in subfolder ', subfolders(i).name, '.']);
end
% Process cath_imp_raw if it's non-empty
if ~isempty(cath_imp_raw)
cath_imp_raw = readcathloc(cath_imp_raw);
disp(['Processing cath_imp_raw in subfolder ', subfolders(i).name, '...']);
% Further processing of cath_imp_raw (Implement this part later)
else
disp(['No files found for cath_imp_raw in subfolder ', subfolders(i).name, '.']);
end
% Process cath_imp_fil if it's non-empty
if ~isempty(cath_imp_fil)
cath_imp_fil = readcathloc(cath_imp_fil);
disp(['Processing cath_imp_fil in subfolder ', subfolders(i).name, '...']);
% Further processing of cath_imp_fil (Implement this part later)
else
disp(['No files found for cath_imp_fil in subfolder ', subfolders(i).name, '.']);
end
% Process comp_mag if it's non-empty
if ~isempty(comp_mag)
comp_mag = read_computed_mag(comp_mag);
disp(['Processing comp_mag in subfolder ', subfolders(i).name, '...']);
% Further processing of comp_mag (Implement this part later)
else
disp(['No files found for comp_mag in subfolder ', subfolders(i).name, '.']);
end
% Process fil_mag if it's non-empty
if ~isempty(fil_mag)
fil_mag = read_filtered_mag(fil_mag);
disp(['Processing fil_mag in subfolder ', subfolders(i).name, '...']);
% Further processing of fil_mag (Implement this part later)
else
disp(['No files found for fil_mag in subfolder ', subfolders(i).name, '.']);
end
% Step 3: Align tables
non_empty_tables = {elec_data, raw_mag, patch_imp_raw, patch_imp_fil, cath_imp_raw, cath_imp_fil, comp_mag, fil_mag};
non_empty_tables = non_empty_tables(~cellfun(@isempty, non_empty_tables));
alignTables(non_empty_tables{:});
% Step 4: Calculate beci_status_bit
if ~isempty(elec_data) && ~isempty(beci_data)
beci_status_bit = bits(elec_data, beci_data);
disp('Calculated beci_status_bit.');
else
disp('No elec_data or beci_data found. Cannot calculate beci_status_bit.');
end
% Step 5: Concatenate voxel plot data and plot
voxel_plot_data = [raw_mag(:,["tx_51_raw","ty_51_raw","tz_51_raw"]),elec_data(:,["c52z","c52x","c52y"]),beci_status_bit(:,["beci_status_bit"])];
figure()
stackedplot(voxel_plot_data)
end
end
end
Below are the functions of Align Tables.
function alignTables(varargin)
% Check if any input argument is empty
if any(cellfun(@isempty, varargin))
disp('One or more input tables are empty, alignment not performed.');
return;
end
% Extract t_dws values from each table
t_dws_values = cellfun(@(tbl) tbl.t_dws, varargin, 'UniformOutput', false);
% Check if the t_dws values are the same for all tables
if isequal(t_dws_values{:})
disp('No alignment needed, t_dws values are the same for all tables.');
return;
end
% Find common t_dws values across all tables
common_t_dws = intersect(t_dws_values{:});
% Initialize aligned tables structure
aligned_tables = struct();
% Keep only rows with common t_dws values in each table
for i = 1:numel(varargin)
tbl = varargin{i};
[~, idx_common] = ismember(common_t_dws, tbl.t_dws);
aligned_tables.(inputname(i)) = tbl(idx_common, :);
end
% Display aligned tables
disp('Aligned tables:');
disp(aligned_tables);
end
The same function does not execute for some subfolders. All the sub-fodlers have the same type of files. All are CSVs.
I ran into this error for some sub folders. I dont know where I am going worng. any help would be appreciated
Error using duration/intersect
Dot indexing is not supported for variables of this type.
Error in duration/intersect (line 42)
c.millis = intersect(amillis,bmillis,varargin{:});
Error in alignTables (line 123)
common_t_dws = intersect(t_dws_values{:});
Error in processParentFolder2 (line 208)
alignTables(non_empty_tables{:});

Accepted Answer

Avadhoot
Avadhoot on 12 Mar 2024
Hi Bhargav,
I see that you are encountering an error in the alignTables function in your code. This error is occurring because the "intersect" operation is not working as expected with the extracted "t_dws_values". This could happen because the "t_dws_values" are not in the proper format or if they are empty or non-existent in some tables. You can take the following steps to ensure that the error is resolved:
1. Verify the "t_dws" column type:
Ensure that the t_dws column in all the tables is of the same type i.e "duration". You can add a check for that as follows:
% Check if t_dws values are duration objects
if ~all(cellfun(@(x) isa(x.t_dws, 'duration'), varargin))
disp('Error: Not all t_dws columns are of type duration.');
return;
end
2. Handle missing and empty values:
Before executing the "intersect" operation, make sure that all the values are non-empty. You can use any appropriate strategy to handle this. You can add the following check for that:
% Remove empty t_dws values from consideration
t_dws_values = t_dws_values(~cellfun(@isempty, t_dws_values));
if isempty(t_dws_values)
disp('No t_dws values to align.');
return;
end
These checks should help you to figure out the problem in the data, if there is one. If the problem persists then you could try the following solutions:
  • Ensure consistent data in all the subfolders so that the intersection operation is performed properly.
  • You could include error handling in the "alignTables" function to get a proper idea of the cause and source of errors.
  • Use debugging to inspect the contents of "t_dws_values" just before the "intersect" operation.
I hope this helps
  1 Comment
Bhargav
Bhargav on 13 Mar 2024
Thank you for your response. I will get to this and see where I am going wrong

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!