How can I show the time on Y axis on Bar graph or Stem graph with datetick ?

조회 수: 3 (최근 30일)
Martinppp
Martinppp 2018년 2월 21일
답변: Ian Townend 2024년 11월 7일
Hello,
I have a 20x1 cell array and on each cell there is a time in 'MM:SS.FFF'.
I use datenum to convert it in serial number. Now I would like to use a bar graph or stem graph with numeric values in X axis and those time values in Y Axis.
I use datetick after the plot to convert the value in time text but on my graph there is just '00:00.000' on the Y-axis and if I try to plot in plot graph it's working (I have my time values on Y axis).
My code is (with 'h' a 20x1 cell with time values in text):
Format = 'MM:SS.FFF';
z=[1 2 3 4 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20 21];
h1= datenum(h,Format);
bar(z,h1)
datetick('y',Format)
Do you have a solution for this problem?
Thank you,
Martin

답변 (1개)

Ian Townend
Ian Townend 2024년 11월 7일
I have just been faced with the same problem. My solution was as follows:
%input y_values as datetime
startdate = min(y_values); %smallest datetime
plotvalues = y_values-startdate; %convert to durations
%create bar plot
bar(ax,x,plotvalues) %ax is handle to axes
y_ticks = ax.YTick; %get the plot ticks as durations
dtime = startdate+y_ticks; %convert to datetime
yticklabels(datestr(dtime)) %relabel as date strings
This was sufficient for my purposes.

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by