필터 지우기
필터 지우기

Loading files with a particular string on its name

조회 수: 205 (최근 30일)
Ramiro Rea
Ramiro Rea 2017년 8월 16일
댓글: mirza 2019년 11월 3일
Hi everyone,
I have a series of files named like this:
Result_2017_5_24_13_44_IC.mat
Result_2017_5_24_13_54_MID.mat
Result_2017_5_25_13_20_SVO.mat
Result_2017_5_28_16_50_IC.mat
Result_2017_5_28_16_58_MID.mat
Result_2017_5_28_17_02_SVO.mat
I would like to load the files that have SVO, MID, and IC on its name separately. Is there a way of doing this? Right now I had to change the names of the files to something more simple to process. However, I would like to know a way of working with the files in their original name. The code I have right now works with the modification I do to the name:
for i=1:subjects
files = [num2str(i) '_SVO.mat']; %name of the files to process
load(files);
...
end
Many thanks, Ramiro

채택된 답변

Image Analyst
Image Analyst 2017년 8월 16일
% Specify the folder where the files live.
myFolder = pwd; % or 'C:\Users\yourUserName\Documents\My Pictures' or whatever...
% 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, 'Result*.mat'); % 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);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
thisStructure = load(fullFileName)
end
msgbox('Done');
  댓글 수: 1
Ramiro Rea
Ramiro Rea 2017년 8월 16일
Thanks for this, it has the lines I needed to work around a solution. The code in the end is:
roothpath = '/home/dantes/Dropbox/Graduate/Lab/Studies/Colaborative Project Korea/Results/KAIST';
fpattern = fullfile(roothpath, '/**/*SVO.mat');
files = dir(fpattern);
for k = 1 : length(files)
fName = files(k).name;
fFolder = files(k).folder;
fullname = fullfile(fFolder,fName);
load(fullname)
...
end
Again, thanks a lot!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

mirza
mirza 2019년 11월 2일
편집: Image Analyst 2019년 11월 2일
How do I select "Result_2017_5_24_13_44_IC.mat" file if the values in the title are the variables.
For example, if
year=2014;Month=5; Day=24; x=13; y=44; z=IC
and we have to execute like this,
load(Result_year_Month_Day_x_y_z.mat)
Kindly, update me in this regard.
  댓글 수: 2
Image Analyst
Image Analyst 2019년 11월 2일
year = 2014;
Month = 5;
Day = 24;
x = 13;
y = 44;
z = IC; % z is a character array, not a number.
% Example: " Result_2017_5_24_13_44_IC.mat"
baseFileName = sprintf('Result_%d_%d_%d_%d_%d_%s.mat', year, Month, Day, x, y, z)
fullFileName = fullfile(folder, baseFileName)
if ~isfolder(fullFileName)
message = sprintf('File not found:\n%s', fullFileName);
uiwait(warndlg(message));
return;
end
s = load(fullFileName)
mirza
mirza 2019년 11월 3일
Excellent and Thanks, i never thinked such quick reply. Stay blessed

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by