HOW TO INSERT THE DATE (DATETIME) IN THE TITLE?

66 visualizzazioni (ultimi 30 giorni)
clear all; clc;
%% parte dedicata a lettura dati
ncfile='corrente1.nc';
ncinfo(ncfile);
ncdisp(ncfile);
x1=ncread(ncfile,'lon');
y1=ncread(ncfile,'lat');
time= ncread(ncfile,'time');
time1=(time*60)-(2208988800') ;
date_matlab = datetime (time1, 'convertfrom','posixtime');
formatOut = 'ddmmyyyy';
tt=datestr(date_matlab,formatOut);
io=num2str(tt)
figure
for t=1
startLoc = [1 1 1 t];
count = [Inf Inf 1 1];
d = ncread(ncfile,'uo',startLoc,count);
d=d';
startLoc = [1 1 1 t];
count = [Inf Inf 1 1];
r = ncread(ncfile,'vo',startLoc,count);
r=r';
clf
rob=((d.^2)+(r.^2)).^(1/2);
mymap=pcolor(x1,y1,rob)
mymap.EdgeAlpha=0
hold on
load coast
plot(long,lat+0.1,'black')
q=quiver(x1,y1,d,r,100,'black')
c=q.AutoScaleFactor
q.AutoScaleFactor=1.8
c=q.MaxHeadSize
q.MaxHeadSize=100
sr=colorbar
caxis([0 0.3])
sr.Label.String = 'm/s';
xlabel('longitudine')
ylabel('latitudine')
title(num2str(time(t)))
%print(['tempesta 28072019 [m s^-1] ' num2str(time(t)) '.png'],'-dpng');
VALOREMASSIMO=max(max(r))
end
  2 Commenti
Walter Roberson
Walter Roberson il 14 Dic 2020
formatOut = 'ddmmyyyy';
tt=datestr(date_matlab,formatOut);
io=num2str(tt)
You do not use io after that point, so you should not assign to it.
Once you have removed that assignment, you will not be using tt after the assignment to it, so remove that line too.
Once you have done that, you will not be using formatOut anywhere, so delete that too.
In other words, delete all three of those lines; you do not need them.
Walter Roberson
Walter Roberson il 14 Dic 2020
title(num2str(time(t)))
I believe what you should be using instead is
title(string(date_matlab(t)))
You can set the Format property of date_matlab to reflect the output format you want -- probably 'ddMMyyyy' if it is to be the same format you were using for tt . Caution: datestr() uses mm for months but datetime() uses MM for months.

Accedi per commentare.

Risposta accettata

Image Analyst
Image Analyst il 14 Dic 2020
Here's demo to put the date/time of a file, and the current date/time into a caption over an axes.
Adapt as needed, like if you want only the time or only the date. If you can't, then attach your variables in a .mat file.
% Demo to put the date/time of a file, and
% the current date/time into a caption over an axes.
% Specify some file.
fileName = '1.jpg'; % Whatever.
% Get the date of the file.
dirListing = dir(fileName)
% Get the current time.
tNow = datestr(now)
% Create a plot or image or something
imshow(peaks(300), []);
% Create a title
caption = sprintf('File Date = %s. Right now it is %s',...
dirListing.date, tNow);
title(caption, 'FontSize', 12);

Più risposte (1)

Steven Lord
Steven Lord il 14 Dic 2020
peaks
z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ... - 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ... - 1/3*exp(-(x+1).^2 - y.^2)
title("It is currently " + string(datetime('now')))
  2 Commenti
Brian Whatcott
Brian Whatcott il 21 Lug 2023
I want you to know that to a person who has spent several hours attempting to place a passed name and the current date and time in a title, your offering comes as a cool draft to a desert traveller. Thank you. O Lord!
Image Analyst
Image Analyst il 22 Lug 2023
@Brian Whatcott like I showed in my answer, you can use sprintf() to make up virtually any title/caption for your axes that you want.
% Create a title
caption = sprintf('File Date = %s. Right now it is %s',...
dirListing.date, tNow);
title(caption, 'FontSize', 12);
You can put whatever variable values in that you want.

Accedi per commentare.

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by