Adding non-fixed labels to UIAxes

조회 수: 5 (최근 30일)
Mehmet
Mehmet 2023년 12월 29일
댓글: Adam Danz 2023년 12월 29일
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.

채택된 답변

Dinesh
Dinesh 2023년 12월 29일
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
Mehmet
Mehmet 2023년 12월 29일
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 2023년 12월 29일
It's not clear why you can't use text() or datatip() to achieve the goal.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by