MATLAB: Applying Function to Structure

functionstructures

Currently, here is my code:
% Specify the folder where the files live.
myFolder = 'C:\Users\Irwin\Desktop\Matlab\Scintillator_project\advanced';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s',
myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.spe');
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
end
I currently have a structure in which each entry contains a .spe file. I would like to apply the function readSPE https://www.mathworks.com/matlabcentral/fileexchange/35940-readspe to each entry in the structure to convert them from .spe format to a 3D array.
Please help!
Thanks 🙂

Best Answer

In terms of your existing code, you would just add a readSPE(fullFileName) after your fprintf().
If you want to use more compact code then
filePattern = fullfile(myFolder, '*.spe');
dinfo = dir(filePattern);
filenames = fullfile(myFolder, {dinfo.name});
output = arrayfun(@readSPE, filenames, 'uniform', 0);