For Loop doesn't execute for some specific folders.
Afficher commentaires plus anciens
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{:});
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Function Creation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!