Setting x axis in hh:mm format for plotting

조회 수: 2 (최근 30일)
Sreeraj T
Sreeraj T 2020년 6월 18일
댓글: Sreeraj T 2020년 6월 19일
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)
Sreeraj T
Sreeraj T 2020년 6월 19일
Mehmed Saad, thank you..

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ajith Krishna Kanduri
Ajith Krishna Kanduri 2020년 6월 18일
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에서 Performance and Memory에 대해 자세히 알아보기

태그

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by