Setting x axis in hh:mm format for plotting

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

Accepted Answer

Mehmed Saad
Mehmed Saad on 18 Jun 2020
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 Comments
Mehmed Saad
Mehmed Saad on 19 Jun 2020
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)

Sign in to comment.

More Answers (1)

Ajith Krishna Kanduri
Ajith Krishna Kanduri on 18 Jun 2020
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.

Categories

Find more on Performance and Memory in Help Center and File Exchange

Tags

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!