Filter löschen
Filter löschen

Adding non-fixed labels to UIAxes

6 Ansichten (letzte 30 Tage)
Mehmet
Mehmet am 29 Dez. 2023
Kommentiert: Adam Danz am 29 Dez. 2023
Hello, again !
I designed an app that has dates on x-axis and values on y-axis. As i expected, there are some drastical changes on the values between two following dates. So, i want to add the reason of critical changes onto the line between these two points like "Something happened in this section.". I tried to add labels and assign tooltips to them but the labels are sliding by zooming in/out as they have fixed position. Hence i'm trying to find a way that provides the information box wherever the two points are. Additionally, if there is a method that i also can handle the visibility of this information box, i would appreciate more.
Thanks.

Akzeptierte Antwort

Dinesh
Dinesh am 29 Dez. 2023
Hi Mehmet.
To address the issue of adding interactive annotations to your app's plot that align with significant data changes and remain positioned correctly during zooming or other operations, use MATLAB's "text" function. This function allows you to specify the position of your annotations in data coordinate terms, ensuring they move appropriately with your plot. You can also manage the visibility of these annotations by toggling the 'Visible' property of the text object based on user interaction or specific app conditions.
The following is an example:
x = 1:10; % Sample date points
y = rand(1, 10) * 20; % Sample values
figure;
plot(x, y, '-o');
idx1 = 5; % Index of first date point
idx2 = 6; % Index of second date point
% Calculate midpoint so that text can be placed there
midX = mean([x(idx1), x(idx2)]);
midY = mean([y(idx1), y(idx2)]);
% Use 'text' to create the label
txt = 'Something happened in this section';
text(midX, midY, txt, 'HorizontalAlignment', 'center', 'BackgroundColor', 'white');
The text's position is not fixed when you zoom in/out in this case. The label will always be between these 2 date points.
The following is the link to the documentation:
  2 Kommentare
Mehmet
Mehmet am 29 Dez. 2023
Thanks for the info but this does not work on my project. The callback that refresh my plot is in the following:
cla(app.UIAxes)
hold(app.UIAxes,'on')
axis(app.UIAxes,'tight')
xdata = app.Data.Date; %getting data under date column
ydata = app.Data.Sales; %getting data under sales column
filterdata(app) % filter function that filter my background table
for ii = length(colors)
% app.display elements are being displayed after multiple choices
% colors is filtering product colors
choice = ((app.Data.Products = colors(ii)) & (app.display))
plot(app.UIAxes,xdata((choice)),ydata(choice));
end
annotate(app)
drawnow;
I extracted this from my code but some lines may not confirm. This is a well-executable code.
This may help to get the solution.
Thanks again!!
Adam Danz
Adam Danz am 29 Dez. 2023
It's not clear why you can't use text() or datatip() to achieve the goal.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by