Import Data Into Workspace Issue

1 view (last 30 days)
Hello,
I need some help with the following situation:
1) I have a folder (bkg_folder) containing numerous .spe files.
2) I have a function (readSPE) which can read a .spe file and output a 3D matrix of values
3) I am trying to create a script which imports all the .spe files in bkg_folder into matlab and stores them as individual matrices. I'd like the names of these matrices to match the names of the corresponding files.
Here is what I have so far:
%Specify folder
bkg_folder = 'C:\Users\Irwin\Desktop\Matlab\photon_count_project\Background';
%Ensure folder exists
if ~isdir(bkg_folder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', bkg_folder);
uiwait(warndlg(errorMessage));
return;
end
%Generate List of All Files in Folder
bkgPattern = fullfile(bkg_folder,'*.spe');
bkg_files = dir(bkgPattern);
for i = 1:length(bkg_files)
basebkgName = bkg_files(i).name;
fullbkgName = fullfile(bkg_folder, basebkgName);
fprintf(1, 'Now reading %s\n', fullbkgName);
image = readSPE(fullbkgName);
end
The issue I am encountering is having the readSPE function save each .spe file to a separate 3D matrix (which would be named the same as its corresponding file).
Please help, let me know if anymore information is necessary!
Thanks :)

Accepted Answer

Nagarjuna Manchineni
Nagarjuna Manchineni on 21 Jun 2017
You can use 'fileparts' command to get the name of the file:
[pathstr,name,ext] = fileparts(filename); % Read the file name
Process/read the data from the file
eval([matlab.lang.makeValidName(name) '=' data ';']); % Assign the value to a variable named 'filename'
See the following documentation link for more information:

More Answers (0)

Community Treasure Hunt

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

Start Hunting!