Import Data Into Workspace Issue

조회 수: 1 (최근 30일)
itend
itend 2017년 6월 13일
답변: Nagarjuna Manchineni 2017년 6월 21일
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
Nagarjuna Manchineni 2017년 6월 21일
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에서 Import, Export, and Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by