Matching multiple file names

10 次查看(过去 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