Matching multiple file names
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
0 投票
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.
採用された回答
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 件のコメント
Pouya
2022 年 4 月 19 日
Hi Chris,
Thank you for a quick response.
filesToOpen succefully recognizes the files corresponding to the selected magnetic field file. How can I use the logical array (fielsToOpen) to actually open those chosen files?
By the way here is the small block of code that I have so far for your refrence:
clc
clear
path = 'C:\Users\pouya\OneDrive\Desktop\SW\Plasma\Processed\2014';
addpath 'C:\Users\pouya\OneDrive\Desktop\SW\Mag\Processed\2014\Alpha dB_e';
magstr = "SW_OPER_MAGA_HR_1B_20140101T000000_20140101T235959_0505_MDR_MAG_HR_processed.mat";
pat = "_" + digitsPattern(8) + "T";
thedates = extract(magstr,pat);
eFieldNames = {dir(path).name};
% eFieldFnames = {'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(eFieldNames,thedates(1));
Regards,
Pouya.
Chris
2022 年 4 月 19 日
I would probably cd(path), to get to the appropriate directory. I would also amend one of the previous commands:
filesToOpen = find(contains(eFieldNames,thedates(1)));
cd(path)
for idx = 1:numel(filesToOpen)
% May vary depending on how you open the file...
temp = readmatrix(eFieldNames(filesToOpen{idx}));
% Put the data somewhere more useful
allEfields{idx} = temp;
end
Do not use CD to access data files. Using CD is slow (it forces MATLAB to rescan the directories for MATLAB files), makes debugging harder, and is superfluous.
All MATLAB function that import/export data files accept absolute/relative filenames. Using relative/absolute filenames is easy and efficient. You should use absolute/relative filenames, for which FULLFILE is very useful.
Pouya
2022 年 4 月 19 日
Chris, Stefan,
Both return "Brace indexing is not supported for variables of this type." (when I replace cd with fullfile too)
Is there more to the error message?
Does the fully constructed path appear correct? You might put a breakpoint inside the for loop and inspect the filename you are trying to open/import.
Additionally, I would rename your "path" variable, as path() is a Matlab function that overlaps with the things you are trying to do here.
Pouya
2022 年 4 月 19 日
I can add that it refers to temp in the for loop. Basically:
Brace indexing is not supported for variables of this type.
Error in analysisstarttest (line 24)
temp = readmatrix(eFieldNames(filesToOpen{idx}));
(analysisstarttest is my script's name)
Chris
2022 年 4 月 19 日
I suspect you're missing a backslash.
You can either insert a breakpoint, as I suggested, or display the filename before trying to open it:
disp("File Name: " + eFieldNames(filesToOpen{idx}))
Pouya
2022 年 4 月 19 日
Chris,
Thank you for continous help.
Unfortunatly now I am getting the same error but this time for disp
clc
clear
path = 'C:\Users\pouya\OneDrive\Desktop\SW\Plasma\Processed\2014\';
addpath ('C:\Users\pouya\OneDrive\Desktop\SW\Mag\Processed\2014\Alpha dB_e');
magstr = "SW_OPER_MAGA_HR_1B_20140102T000000_20140102T235959_0505_MDR_MAG_HR_processed.mat";
pat = "_" + digitsPattern(8) + "T";
thedates = extract(magstr,pat);
eFieldNames = {dir(path).name};
filesToOpen = find(contains(eFieldNames,thedates(1)));
% f=fullfile('processed','2014','filesToOpen');
cd(path)
for idx = 1:numel(filesToOpen)
% May vary depending on how you open the file...
disp("File Name: " + eFieldNames(filesToOpen{idx}));
% temp = readmatrix(eFieldNames(filesToOpen{idx}));
% % Put the data somewhere more useful
% allEfields{idx} = temp;
end
Which leads to:
Brace indexing is not supported for variables of this type.
Error in analysisstarttest (line 24)
disp("File Name: " + eFieldNames(filesToOpen{idx}));
Pouya
2022 年 4 月 19 日
I'm suspecting the problem is originating from eFieldNames = {dir(path).name};
eFieldNames has two elements at it's start that are "." and ".."
Chris
2022 年 4 月 19 日
filesToOpen = find(contains(eFieldNames,thedates(1)));
will not indicate those two files, because the pattern you are looking for is not found there. The smallest index possible in filesToOpen should be 3.
Chris
2022 年 4 月 19 日
@Pouya Pourkarim I just looked back at your most recent code. When you perform the dir() command, apparently you cannot have the trailing backslash.
I would reconstruct the full filenames like so:
pathn = 'C:\Users\pouya\OneDrive\Desktop\SW\Plasma\Processed\2014';
for (...)
% filesep is a Matlab command that generates the appropriate
% separator for your OS
% None of these are strings, so do char concatenation with []
f = [pathn, filesep, eFieldNames(filesToOpen{idx})];
disp("File Name: " + f)
end
Pouya
2022 年 4 月 19 日
Hi Chris
Unfortunatly the same error still presists and this time is
Brace indexing is not supported for variables of this type.
Error in analysisstarttest (line 24)
f=[path, filesep, eFieldNames(filesToOpen{idx})];
Let's think about it in a different way. I'm thinking I can do this process for mag files that have multiple E files manually and let the code do it only for mag files that have only one corresponding E files. If you come up with ideas I will be grateful for the help but I don't want to consume too much of your time now.
Regards,
Pouya.
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.
Thanks, it actually solved the problem with the Brace indexing error that I was getting.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Characters and Strings についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
