我有这样的代码:
clc;clear;close all
%%
Time=linspace(16.8,17.8,230400)';
Field=linspace(50,145,230400)';
figure('units','normalized','outerposition',[0 0 1 1])
plot(Time,Field)
%%
figure('units','normalized','outerposition',[0 0 1 1])
hammng_wndw_size=4096;
window=hamming(hammng_wndw_size); %window size
noverlap=512; % the noverlaps its the no. of points for repeating the window
nfft=4096; %size of fft
fs=32; %sampling freq
[Sp,F,T,P]=spectrogram(Field,window,noverlap,nfft,fs,'yaxis');
T_forspectrogrm=T./3600+Time(1);
surf(T_forspectrogrm,F,10*log10(P),'edgecolor','none','FaceColor','interp');
axis tight;ylim([0 4]);view(0,90);
colormap(jet);colorbar;
x轴是时间,y轴是其他数量,可以说是字段。现在,当我绘制时,第一个图形的x轴从16.8到17.8,第二个图形的x轴从16.8和18.8开始。实际上,这对应于16:48:00到17:48:00,第二个相似。我该如何修改程序以将x轴转换为hh:mm格式?
我以这种方式尝试过
TimeInReqFrmat=datestr(Time(:,1),'HH:MM:SS')
,但这给了我一个字符串。
我正在使用Matlab 2016a。
预先感谢
答案 0 :(得分:1)
您的方法几乎是正确的。您需要使用datetimes
。将十进制数字转换为日期时间有点棘手
x = [16.8;17.8]
H = floor(x); % hour
m = floor((x-H)*60); % minute
S = (x-H)*60-m; % second
% create date vector
DateVec = datetime(0,0,0,H,m,S);
plot(DateVec,rand(size(DateVec)))
% set the tick format
xtickformat('HH:mm')
设置xtickformat
的选项很多。
如果您使用的是unix时间,则可以立即进行转换
datetime(x,'ConvertFrom','datenum')
如果您只有时间,则需要为正确的datetime
确定一个日期。另外,您还可以考虑使用duration
。