Matching multiple file names

조회 수: 11 (최근 30일)
Pouya
Pouya 2022년 4월 19일
편집: Pouya 2022년 4월 19일
Hi
I have two types of data each in a seperate folder. Lets say I have magnetic field files in folder x and electric field files in folder y.
There are 365 magnetic field files each represnting one day of measurments. However electric field files are more than 365 and there are multiple files per day. For example, we have electric field files measured from 00:00:00 to 10:30:00 in one file for a day and then 11:00:00 to 23:59:59 in another file for the same day. Note that for some days there are more than two electric field files.
I want MATLAB to open magetic field file
"SW_OPER_MAGA_HR_1B_20140101T000000_20140101T235959_0505_MDR_MAG_HR_processed.mat" from it's folder
then open electric field files
"SW_EXPT_EFIA_TCT16_20140101T000000_20140101T103000_0302_processed.mat"
and
"SW_EXPT_EFIA_TCT16_20140101T110000_20140101T235959_0302_processed.mat"
which belong to the same day as magnetic field file as shown by data timestamp in file names. I was not succeful in using regexp and dir functions.
Thanks in advance,
Pouya.

채택된 답변

Chris
Chris 2022년 4월 19일
편집: Chris 2022년 4월 19일
You'll have to fill in some of the blanks because I don't know how you're opening files, but to find the corresponding filenames in the electric field folder...
magstr = "SW_OPER_MAGA_HR_1B_20140101T000000_20140101T235959_0505_MDR_MAG_HR_processed.mat";
pat = "_" + digitsPattern(8) + "T";
thedates = extract(magstr,pat);
% eFieldNames = {dir().name};
eFieldNames = {'SW_EXPT_EFIA_TCT16_20140101T000000_20140101T103000_0302_processed.mat',
'SW_EXPT_EFIA_TCT16_20140102T110000_20140102T235959_0302_processed.mat',
'SW_EXPT_EFIA_TCT16_20140101T110000_20140101T235959_0302_processed.mat'};
filesToOpen = contains(eFieldFnames,thedates(1))
filesToOpen = 3×1 logical array
1 0 1
  댓글 수: 14
Image Analyst
Image Analyst 2022년 4월 19일
편집: Image Analyst 2022년 4월 19일
@Pouya Pourkarim filesToOpen is a linear vector - a regular array that needs parentheses, not a cell array that needs braces. So whenever you see
filesToOpen{idx} % Won't work.
replace it with
filesToOpen(idx) % Should work.
Do NOT use path as the name of a variable since that's an important built in variable. Call it "folder" or something other than path.
Don't do this
f=[path, filesep, eFieldNames(filesToOpen{idx})];
Use fullfile() instead. And use descriptive variable names, otherwise your program will become an alphabet soup mess of cryptics one and two letter variables. And eFieldNames is a cell array so you need braces:
fullFileName = fullfile(folder, eFieldNames{filesToOpen(index)});
See the FAQ for a good discussion on where to use braces, brackets, or parentheses.
Pouya
Pouya 2022년 4월 19일
편집: Pouya 2022년 4월 19일
Thanks, it actually solved the problem with the Brace indexing error that I was getting.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by