Matching multiple file names

8 views (last 30 days)
Pouya
Pouya on 19 Apr 2022
Edited: Pouya on 19 Apr 2022
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.

Accepted Answer

Chris
Chris on 19 Apr 2022
Edited: Chris on 19 Apr 2022
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 Comments
Image Analyst
Image Analyst on 19 Apr 2022
Edited: Image Analyst on 19 Apr 2022
@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 on 19 Apr 2022
Edited: Pouya on 19 Apr 2022
Thanks, it actually solved the problem with the Brace indexing error that I was getting.

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!