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