Setting x axis in hh:mm format for plotting

3 次查看(过去 30 天)
I have a code which goes like this:
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;
The result of this plot is these two figures:
The xaxis is time and y axis is some other quantity, lets say field. Now, when I plotting, the x axis starts from 16.8 to 17.8 for first figure and 16.8 and 18.8 for second figure. This actually corresponds to 16:48:00 to 17:48:00 and similarly for the second one. How do I have to modify the program to convert the x-axis into hh:mm format do this job?
I tried in this fashion
TimeInReqFrmat=datestr(Time(:,1),'HH:MM:SS');
but this gives me a string of characters.
I am using Matlab 2016a.
Thanks in advance

采纳的回答

Mehmed Saad
Mehmed Saad 2020-6-18
The Dumb Method
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)
%% Edited by Mehmed
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ax =gca;
ax.XTickLabel=datestr(hours(ax.XTick),'HH:MM:SS');% Edited by Mehmed
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
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;
%% Edited by Mehmed
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ax =gca;
ax.XTickLabel=datestr(hours(ax.XTick),'HH:MM:SS');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3 个评论
Mehmed Saad
Mehmed Saad 2020-6-19
You can also try this
% Changing time to duration directly
Time =duration(hours(linspace(16.8,17.8,230400)'),'format','hh:mm:ss');
Field=linspace(50,145,230400)';
figure('units','normalized','outerposition',[0 0 1 1])
plot(Time,Field)

请先登录,再进行评论。

更多回答(1 个)

Ajith Krishna Kanduri
Hi Sreeraj,
Please refer to the documentation of datestr function (https://in.mathworks.com/help/matlab/ref/datestr.html). I think the arguments you are passing into the constuctor doesn't match any syntax. You can either write another function that can change Time array as you mentioned and then pass that string array into the constructor or instead you could directly create a dateformat array that could be used.

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

标签

产品


版本

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by