save filename by values using a variable

17 views (last 30 days)
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

Accepted Answer

KSSV
KSSV on 3 Jun 2021
Edited: KSSV on 3 Jun 2021
for j = for j = 3601:length(time)
thedate = string(time3(j)) ;
tiffile = strcat('Y_',thedate,'_',ncfile,'.tif') ;
end

More Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 3 Jun 2021
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

Categories

Find more on Agriculture in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!