Read file whose name has a pattern that is part of another file name pattern

3 ビュー (過去 30 日間)
Meir Zeilig Hess
Meir Zeilig Hess 2022 年 1 月 14 日
コメント済み: Ive J 2022 年 1 月 14 日
I have files with two patterns in their names: "...daily.nc" and "...4xdaily.nc".
Currently I'm using:
filePattern = fullfile(Exp_Path,strcat('*',Sampling_Rate,'.nc'));
ncFiles = dir(filePattern);
with Sampling_Rate being a string (either 'daily' or '4xdaily'), and with Exp_Path being the directory containing the relevant files.
For the "4xdaily" pattern, this works properly, giving me a structure with all the requested files and only them.
When I try to apply this to the the "daily" pattern, I get a structure with both kinds of files (which makes sence, as the string 'daily' is included in the larger string '4xdaily').
Is there a way to obtain only the "daily" files?

採用された回答

Ive J
Ive J 2022 年 1 月 14 日
編集済み: Ive J 2022 年 1 月 14 日
Try this
files = {dir("*daily.nc").name}.'
{'data1.4xdaily.nc'}
{'data12.daily.nc' }
{'data2.4xdaily.nc'}
{'data3.daily.nc' }
xdailyIdx = endsWith(files, '4xdaily.nc');
xdailyf = files(xdailyIdx)
{'data1.4xdaily.nc'}
{'data2.4xdaily.nc'}
dailyf = files(~xdailyIdx)
{'data12.daily.nc'}
{'data3.daily.nc' }
  2 件のコメント
Meir Zeilig Hess
Meir Zeilig Hess 2022 年 1 月 14 日
After a small modification, it worked!
I only needed to replace the first line of your answer with:
files = {dir(filePattern).name}; % No need for transposition, but the full path was required
Then, using the index array "xdailyIdx", it was easy to manipulate not only the array of file names, "files", but also the whole structure "ncFiles".
Thank you very much!
Ive J
Ive J 2022 年 1 月 14 日
You are right, that's just old habits, I hate row vectors ;)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by