Filter löschen
Filter löschen

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 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Voss
Voss am 16 Dez. 2023
Bearbeitet: Voss am 16 Dez. 2023
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

Weitere Antworten (0)

Kategorien

Mehr zu Simulink Environment Customization finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by