how to extract specific numbers from string?

5 ビュー (過去 30 日間)
Lilya
Lilya 2020 年 3 月 12 日
回答済み: Luna 2020 年 3 月 12 日
Hi all- I have to access the file name (MERRA2_100.tavg1_2d_slv_Nx.19800101.nc4.nc4) to be able to read the data.
Accordingly, I've created the following code to do that. But the problem is the determination of the substring location (in bold) as it should be varied due to the multiple numbers of files I have. So what I need is to determine the location of the day and month in 19800101.
any help would be appreciated.
for i=1:length(all_files)
fname=all_files(i).name;
tmp = split(fname,'.');
mdate = tmp(3);
mdate = str2double(mdate);
year = round(mdate/10000);
month = round((mdate - (year*10000)));
mm = round(month/100);
dd = round(month - mm*100);
if (dd == iday) && (mm == imon);
filelist(j) = strcat(all_files(i).folder,'/' ,fname);
j = j + 1;
end
end

採用された回答

Luna
Luna 2020 年 3 月 12 日
Hello Lola,
You can convert to datetime object and use the properties like below:
mdate = '19800101';
tmp_date = datetime(mdate,'Format','uuuuMMdd');
Day = tmp_date.Day;
Month = tmp_date.Month;
Year = tmp_date.Year;
I recommend not to use year, month, day as variables they are already built-in functions in Matlab.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by