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 Center および File ExchangePerformance and Memory についてさらに検索

タグ

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by