save filename by values using a variable

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

 Réponse acceptée

KSSV
KSSV le 3 Juin 2021
Modifié(e) : KSSV le 3 Juin 2021
for j = for j = 3601:length(time)
thedate = string(time3(j)) ;
tiffile = strcat('Y_',thedate,'_',ncfile,'.tif') ;
end

Plus de réponses (1)

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

Catégories

En savoir plus sur Functions dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by