MATLAB Answers

save filename by values using a variable

1 ビュー (過去 30 日間)
Manoj Hari
Manoj Hari 2021 年 6 月 3 日
コメント済み: Manoj Hari 2021 年 6 月 3 日
Hai..!!
I would like to save my output filenames based on the values from a variable. For example, I managed to get the name as "Y_3601_file1.tif" where as I need the name to be "Y_2000-01-01_file1.tif". As you can see from the code that the dates are in desired format as a variable "time3". It would be highly appreciable if someone help me to fix this.
clear, clc, close all
ncfiles = dir('*.nc') ;
N = length(ncfiles) ;
for i = 1:N
ncfile = ncfiles(i).name ;
%% Variables %%
lat = double(ncread(ncfile,'lat')) ;
lon = double(ncread(ncfile,'lon')) ;
time = double(ncread(ncfile,'time')) ;
var = double(ncread(ncfile,'var')) ;
%% Time Conversion %%
time2 = daynoleap2datenum(time, 1700, 'dt');
time3 = datetime(time2, 'InputFormat','dd-MMM-yyyy HH:mm:ss', 'Format','dd-MM-yyyy');
%% specific date selection
for j = 3601:length(time)
A = squeeze(var(:,:,j))*3.154e+7;
A = A.';
%% Write nc data to geotiff
R = georasterref('RasterSize',size(A),'LatitudeLimits',[min(lat),max(lat)],........
'LongitudeLimits',[min(lon),max(lon)]);
tiffile = strcat('Y_',num2str(j),'_',ncfile,'.tif') ;
geotiffwrite(tiffile,A,R)
end
end

採用された回答

KSSV
KSSV 2021 年 6 月 3 日
編集済み: KSSV 2021 年 6 月 3 日
for j = for j = 3601:length(time)
thedate = string(time3(j)) ;
tiffile = strcat('Y_',thedate,'_',ncfile,'.tif') ;
end
  1 件のコメント
Manoj Hari
Manoj Hari 2021 年 6 月 3 日
Thank you. It works.

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

その他の回答 (1 件)

Bjorn Gustavsson
Bjorn Gustavsson 2021 年 6 月 3 日
Ok, that should be possible. Someting like this:
% First lets get you the date-string-component in your desired format (guessing year-month-day-Hour-Minute)
time3 = datetime(time2, 'InputFormat','dd-MMM-yyyy HH:mm:ss', 'Format','yyyy_mm_dd_HH_MM');
% Then for the file-name:
tiffile = strcat('Y_',char(time3),'_',ncfile,'.tif');
HTH
  1 件のコメント
Manoj Hari
Manoj Hari 2021 年 6 月 3 日
Thank you. It helped me.

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by