필터 지우기
필터 지우기

How to add data to cursor tip, BUT each piece of data is from a separate computation or table lookup. (Not the idx, not the coordinates.)

조회 수: 4 (최근 30일)
This works except ... not sure how to get the data I want to show in the 'Payoff' label at the data point.
If you can solve this, genius. I really appreciate your help. DC
% Curse02.m
% Add data to where the cursor point is on a figure.
clearvars; close all; clc;
% Example data
x = 1:10;
y = randi([1, 20], 1, 10);
% Here I just created 10 random numbers to use as an example of the data I
% want to show at the cursor tip on hover-over. These values could be stored in a table or generated in real time by a function I use to generate them. I tried to put these into the 'extraInfo' but that is where I fail.
for i = 1:length(x)
Payoff(i) = rand;
end
% Create a figure and plot the data
figure(1);
plot(x, y, 'marker','o','MarkerSize',20,'MarkerEdgeColor','g','linestyle','none');
xlabel('X-axis');
ylabel('Y-axis');
title('Data Cursor with Additional Information');
hold on
% add index labels to the plot
for i = 1:length(x)
text(x(i), y(i), num2str(i), 'HorizontalAlignment', 'center')
end
% Define the data cursor
dcm_obj = datacursormode(gcf);
set(dcm_obj, 'UpdateFcn', @myDataCursorCallback); % Set the callback function
% Callback function to display additional information
function txt = myDataCursorCallback(~, event_obj)
pos = get(event_obj, 'Position');
idx = get(event_obj, 'DataIndex');
% Additional information to display
% This labels correctly but puts the index value and not the payoff.
extraInfo = ['A= ', num2str(pos(1)), ', B= ', num2str(pos(2)),...
'Payoff: ', num2str(idx)];
% Instead of index, display the payoff.
% Construct the text to display
txt = {['D:',' ', extraInfo]};
end

채택된 답변

Voss
Voss 2023년 12월 16일
편집: Voss 2023년 12월 16일
One way to do that is to use the array Payoff as an input to myDataCursorCallback. Something like this:
% Curse02.m
% Add data to where the cursor point is on a figure.
clearvars; close all; clc;
% Example data
x = 1:10;
y = randi([1, 20], 1, 10);
% Here I just created 10 random numbers to use as an example of the data I
% want to show at the cursor tip on hover-over. These values could be stored in a table or generated in real time by a function I use to generate them. I tried to put these into the 'extraInfo' but that is where I fail.
Payoff = rand(1,numel(x));
% Create a figure and plot the data
figure;
plot(x, y, 'marker','o','MarkerSize',20,'MarkerEdgeColor','g','linestyle','none');
xlabel('X-axis');
ylabel('Y-axis');
title('Data Cursor with Additional Information');
hold on
% add index labels to the plot
text(x, y, compose('%d',1:numel(x)),'HorizontalAlignment', 'center')
% Define the data cursor
dcm_obj = datacursormode(gcf);
set(dcm_obj, 'UpdateFcn', {@myDataCursorCallback,Payoff}); % Set the callback function, and include additional argument Payoff
% Callback function to display additional information
function txt = myDataCursorCallback(~, event_obj, extraData)
pos = get(event_obj, 'Position');
idx = get(event_obj, 'DataIndex');
% Additional information to display
% This puts the the payoff, which has been sent as input extraData
extraInfo = ['A= ', num2str(pos(1)), ', B= ', num2str(pos(2)),...
', Payoff: ', num2str(extraData(idx))];
% Construct the text to display
txt = {['D:',' ', extraInfo]};
end
  댓글 수: 2
David
David 2023년 12월 16일
Thanks Voss, this works and gives me a better understanding of how that data tip object works. I really appreciate your working on this. Genius ++

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulink Environment Customization에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by