How do I change datetime format in stackedplot on X-Axis?

Right now by default it's in 'hh:mm' I want it to be in 'hh:mm:ss' format with 1 minute interval.
Note: The image shown below is a "stackedplot" and not "plot".
Plot.png

回答(2 个)

In case anyone is still looking for this solution,
% Create stacked plot, set xdata as datetime values
% The XData can also be set using 'XVariable' for tables but needs to be datetime format.
sph = stackedplot(rand(50,5),'XData',datetime(2020,1,1)+minutes(linspace(0,480,50)));
% Get axis handles
ax = findobj(sph.NodeChildren, 'Type','Axes');
% Set datetime format. The first axis will be the bottom one.
xtickformat(ax(1),'HH:mm:ss')
hAx=gca;
hAx.TickLabelFormat='hh:mm:ss';
You can set the XTick values as wanted but there will not possibly be room to label more than half-hour frequency if that when you add the other text--just not enough real estate to have that much text in such close proximity.

5 个评论

Hi dpb,
Thanks for your answer but it still gives me an error shown below:
Unrecognized property 'TickLabelFormat' for class 'matlab.graphics.chart.StackedLineChart'.
I think this would work fine if it was normal plot but as I mentioned it's a "stackedplot".
My R2018b got trashed by a windows update and haven't gotten restored so was presuming there'd be duration or timeruler axis underneath.
From the online doc, looks like they've (again) hidden properties -- you can try the FileExchange https://www.mathworks.com/matlabcentral/fileexchange/32934-getundoc-get-undocumented-object-properties routine from Yair and see if you can go "handle diving" to find properties of interest to let you do what you're looking for. Bound to be there somewhere; it's just uncovering how they're buried and whether the object is or is not totally opaque to the user by TMW's design/implementation.
Hi dpb,
I tried the above function by creating a stackedplot variable 'sp' and it gave me the results shown below. I don't think any of the available properties can be used for the solution. Thanks Again.
getundoc(sp)
ans =
struct with fields:
ApplicationData: [1×1 struct]
Axes: [1×1 Axes]
AxesPropertiesListener: [1×1 event.listener]
AxesVisibleCache: 1
BeingDeleted: 'off'
ChartDecorationInset: [0.0749 0.0432 1.1102e-16 4.4409e-16]
Copyable: 1
DataCursor: [1×1 matlab.graphics.chart.internal.stackedplot.StackedLineDataCursor]
DefaultPropMap_Internal: [0×0 struct]
Description: 'StackedLineChart'
DescriptionMode: 'manual'
DisplayLabelsHandle: [1×1 Text]
DisplayLabelsMode: 'manual'
DisplayVariablesMode: 'auto'
DisplayVariables_I_EC: {'Var1'}
ErrorCallback: ''
ErrorCallbackMode: 'auto'
GapBetweenAxes: 0.1000
HandleVisibilityMode: 'auto'
HitTest: 'on'
InnerVariableIndex: 0
InputType: 'table'
Internal: 0
LegendHandle: [1×1 Legend]
LinePropertiesListener: [1×1 event.listener]
LooseInset: [0.1300 0.1100 0.0950 0.0750]
MarkedCleanListener: [1×1 event.listener]
MaxInsetForSubplotCell: [0 0 0 0]
MaxNumAxes: 25
MaxVarIndex: 1
MessageHandle: [1×1 TextBox]
MinAxesHeight: 20
NodeChildren: [2×1 Graphics]
NodeParent: [1×1 ScribeLayer]
NumPlotsInAxes: 5
OuterPositionPixelsCache: [1 1 1.2653e+03 712.6667]
PanInteraction: [1×1 matlab.graphics.interaction.uiaxes.Pan]
ParentMode: 'manual'
Plots: {[1×5 Line]}
Selected: 'off'
SelectionHandle: [0×0 SelectionHandles]
Serializable: 'on'
SerializableApplicationData: [1×1 struct]
SerializableMode: 'auto'
SkipReupdate: 0
SubplotCellOuterPosition: [0 0 0 0]
Tag: []
TightInset: [0.0749 0.0432 1.1102e-16 4.4409e-16]
TransformForPrintFcn: ''
TransformForPrintFcnImplicitInvoke: 'on'
TransformForPrintFcnImplicitInvokeMode: 'auto'
TransformForPrintFcnMode: 'auto'
Type: 'stackedplot'
UnitsCache: 'normalized'
VariableIndex: 1
VisibleMode: 'auto'
XData_I_EC: [1×0 double]
XLabelHandle: [1×1 Text]
XLabelMode: 'auto'
XLimitsMode: 'auto'
XVariable_I_EC: []
YData_I_EC: []
ZoomInteraction: [1×1 matlab.graphics.interaction.uiaxes.ScrollZoom]
AutoListeners__: {[1×1 event.listener]}
Behavior: [1×1 struct]
getundoc(sp)
ans =
struct with fields:
ApplicationData: [1×1 struct]
Axes: [1×1 Axes]
MaxNumAxes: 25
MaxVarIndex: 1
MessageHandle: [1×1 TextBox]
MinAxesHeight: 20
NodeChildren: [2×1 Graphics]
NodeParent: [1×1 ScribeLayer]
NumPlotsInAxes: 5
...
Plots: {[1×5 Line]}
Selected: 'off'
...
Behavior: [1×1 struct]
Well, there's an Axes handle to start probing... don't give up so quickly!!
Also, try setting the display format for the x-axis variable before plotting -- possibly the function will look at that in deciding the display format.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Annotations 的更多信息

产品

版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by