Applying Function to Structure

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 :)

 Respuesta aceptada

Walter Roberson
Walter Roberson el 21 de Ag. de 2017
Editada: Jan el 21 de Ag. de 2017
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);

6 comentarios

Jan
Jan el 21 de Ag. de 2017
Typo fixed.
itend
itend el 22 de Ag. de 2017
Thank you for your response!
So if I then want to convert the data to type double and apply a median filter across the "3rd" dimension of each matrix (after readSPE has converted the files from .spe to 3D arrays), can I do this:
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.spe'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
readSPE(fullFileName);
double(median(fullFileName,3));
end
Furthermore, how can I call the files if I want to generate an image from them, say using imagesc?
Thanks again!!
itend
itend el 22 de Ag. de 2017
I also wanted to add that when I use your condensed code, I get the following error:
Error using fopen
First input must be a file name or a file identifier.
Error in readSPE (line 231)
fd = fopen(filename,'r');
Error in avdanced2 (line 14)
output = arrayfun(@readSPE, filenames, 'uniform', 0);
Here is the code with your condensed insert:
% 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');
dinfo = dir(filePattern);
filenames = fullfile(myFolder, {dinfo.name});
output = arrayfun(@readSPE, filenames, 'uniform', 0);
What do you think?
Thanks again!!
Sorry, the arrayfun should be cellfun:
output = cellfun(@readSPE, filenames, 'uniform', 0);
filt_order = 3;
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.spe'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
num_files = length(theFiles);
filt_results = cell(num_files, 1);
for k = 1 : num_files
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
this_spe = readSPE(fullFileName);
filt_result{k} = medfilt1(this_spe, filt_order, 3);
end
You cannot use imagesc or anything similar to create images from them. The result of median filtering is going to be 3 dimensional if the input is 3 dimensional. You need to decide how you want to create the images of the 3D arrays:
  • as iso surfaces using isosurface()
  • as slices, using slice()
  • using the R2017a voxelViewer() app
  • as animation
  • as voxels, using some other routine
itend
itend el 22 de Ag. de 2017
Thank you so much for your help!!!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 21 de Ag. de 2017

Comentada:

el 22 de Ag. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by