Cannot set text to UIaxes because cannot set position since xaxis is made of datetime

13 views (last 30 days)
I have an application that plots time against pressure values. Time is of the format datetime. I plot using these lines of code.
plot(app.UIAxes_9,pressureTime,Pressure);
app.UIAxes_9.XGrid ="on";
app.UIAxes_9.XMinorGrid ="on";
app.UIAxes_9.YGrid ="on";
app.UIAxes_9.YMinorGrid ="on";
app.UIAxes_9.LineWidth = 2;
app.UIAxes_9.XLabel.String = 'Time&Date';
app.UIAxes_9.XLabel.FontSize = 12;
app.UIAxes_9.XLabel.FontWeight ="bold";
app.UIAxes_9.YLabel.String = 'Pressure[bar]';
app.UIAxes_9.YLabel.FontSize = 12;
app.UIAxes_9.YLabel.FontWeight ="bold";
app.UIAxes_9.Title.String = 'Pressure Cycle';
app.UIAxes_9.Title.FontSize = 14;
app.UIAxes_9.Title.FontWeight ="bold";
[maxValue, maxIndex] = max(Pressure);
maxY = Pressure(maxIndex);
maxX = pressureTime(maxIndex);
text1 = 'Max Pressure[bar]: ';
text2 = num2str(maxValue);
finalText = strcat(text1,text2);
text('Parent',app.UIAxes_9,'String',finalText,'HorizontalAlignment', 'left','VerticalAlignment','bottom','Position',[maxX maxY]);
The problem is the last line, where I tried to put a text on the UIAxes. That is
text('Parent',app.UIAxes_9,'String',finalText,'HorizontalAlignment', 'left','VerticalAlignment','bottom','Position',[maxX maxY]);
where I tried to set the position of the text at maxX and maxY. But [maxX maxY] is giving me an error :
Error using datetime/horzcat (line 1468)
All inputs must be datetimes or date/time character vectors or date/time strings.
So what I understood is you cant combine a double and datetime to form an array. So how can I do this for a app.UIaxes. For a normal plot it is easy, but here I need for app.UIaxes. How to do this. Thank you.

Accepted Answer

Stephen23
Stephen23 on 6 Jun 2025
Edited: Stephen23 on 6 Jun 2025
The basic approach is to supply an x-value which is a DATETIME object:
figure()
X = datetime(2025,6,1:6);
Y = rand(1,6);
plot(X,Y)
dts = datetime(2025,6,3,12,0,0);
text(dts,0.5, "hello")
For more complex situations (e.g. calling the low-level version of TEXT() which specifies the POSITION as an input vector, as your code shows) you can use RULER2NUM() to get the correct numeric value:
fgh = figure();
axh = axes(fgh);
plot(axh,X,Y)
num = ruler2num(dts,axh.XAxis);
text(Parent=axh, String="world", Position=[num,0.5])

More Answers (0)

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!