Import Data Into Workspace Issue

3 次查看(过去 30 天)
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 :)

采纳的回答

Nagarjuna Manchineni
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:

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by