Two separates custom data tips functions in the same GUI

8 Ansichten (letzte 30 Tage)
In a GUI, I have several axes. How can I create differents custom data tips for each one?
This is just what I've tried so far: a function like this one right here. I've also tried two separates updatefunctions.
% --- Executes on mouse press over axes background.
function graph_5_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to graph_5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
datacursormode(handles.figMainWindow, 'off')
VolDenPotVol = handles.VolDenPotVol;
VolRend = handles.VolRend;
Vol_x_mat = handles.Vol_x_mat;
VolPartNumberHS = handles.VolPartNumberHS;
VolConfigHS = handles.VolConfigHS;
VolCompHS = handles.VolCompHS;
% datacursormode(handles.graph_5, 'on')
% dcm = datacursormode(handles.graph_5);
datacursormode(handles.figMainWindow, 'on') %on
% dcm = datacursormode(gcf);
dcm = datacursormode(handles.figMainWindow);
set(dcm,'UpdateFcn',@(t,e) myupdatefcn_graph5(t,e,VolDenPotVol,VolRend,Vol_x_mat, VolPartNumberHS, VolConfigHS, VolCompHS, 5) );
% --- Executes on mouse press over axes background.
function graph_4_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to graph_4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
datacursormode(handles.figMainWindow, 'off')
VolDenPotPeso = handles.VolDenPotPeso;
VolRend = handles.VolRend;
Vol_x_mat = handles.Vol_x_mat;
VolPartNumberHS = handles.VolPartNumberHS;
VolConfigHS = handles.VolConfigHS;
VolCompHS = handles.VolCompHS;
% datacursormode(handles.graph_5, 'on')
% dcm = datacursormode(handles.graph_5);
datacursormode(handles.figMainWindow, 'on') %on
% dcm = datacursormode(gcf);
dcm = datacursormode(handles.figMainWindow);
set(dcm,'UpdateFcn',@(t,e) myupdatefcn_graph5(t,e,VolDenPotPeso,VolRend,Vol_x_mat, VolPartNumberHS, VolConfigHS, VolCompHS, 4) );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function txt = myupdatefcn(~,event,xdata,ydata,x_mat, PartNumberHS, ConfigHS, Comp, graph)
pos = get(event,'Position');
dts = get(event.Target,'Tag');
[~,j]= find( xdata==pos(1) & ydata==pos(2) );
% D = pdist2(pos(:)',[xdata(:) ydata(:)]);
% [~,j] = min(D);
% xdata(1,:) = xdata(1,:) / 1000;
% j=j(1);
switch graph
case 5
txt = {dts,...
['Densidade de Potência: ', num2str(pos(1)), ' [kW/dm³]'],...
['Rendimento: ', num2str(100 * pos(2)), ' [%]'],...
['Frequência de Chaveamento: ', num2str(xdata(1,j)), ' [Hz]' ],...
['Material do Indutor: ', x_mat{j}]...
['Dissipador: ', PartNumberHS{j}]...
['Comprimento do Dissipador: ', num2str(Comp{j}), '[dm]']...
['Configuração: ', ConfigHS{j}]};
case 4
txt = {dts,...
['Densidade de Potência: ', num2str(pos(1)), ' [kW/kg]'],...
['Rendimento: ', num2str(100 * pos(2)), ' [%]'],...
['Frequência de Chaveamento: ', num2str(xdata(1,j)), ' [Hz]' ],...
['Material do Indutor: ', x_mat{j}]...
['Dissipador: ', PartNumberHS{j}]...
['Comprimento do Dissipador: ', num2str(Comp{j}), '[dm]']...
['Configuração: ', ConfigHS{j}]};
end
end
However, I get this problem when I try to click on the second plot.

Akzeptierte Antwort

Adam Danz
Adam Danz am 17 Apr. 2020
The datacursormode and its update function is applied to the entire figure but you can assign different update function responses to each axes by following these steps listed and demonstrated below.
  1. Assign a unique tag name to each axes so the update function can identify which axes its responding to.
  2. Define the update function after all axes are created and pass any relevant variables into the function.
  3. Within the update function, identify which axes contains the object that was clicked and create the text output according to the active axes.
See inline comments for detail.
% Create figure with 2 subplots
fig = figure();
sp(1) = subplot(2,1,1);
hold on
h(1) = plot(rand(1,20),'-o','DisplayName','A');
h(2) = plot(rand(1,20),'-o','DisplayName','B');
h(3) = plot(rand(1,20),'-o','DisplayName','C');
h(4) = plot(rand(1,20),'-o','DisplayName','D');
h(5) = plot(rand(1,20),'-o','DisplayName','E');
sp(1).Tag = 'subplot1'; % <--- Assign a unique tag to first axes
title('Click to reveal line name')
sp(2) = subplot(2,1,2);
x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
index = 1:numel(x);
sz = 25;
c = linspace(1,10,length(x));
s = scatter(x,y,sz,c,'filled');
sp(2).Tag = 'subplot2'; % <--- Assign a unique tag to second axes
title('Click to reveal index value')
% After plotting, assign data cursor object to figure.
dcm = datacursormode(fig);
dcm.Enable = 'on';
% Create a structure that contains fields to any variable
% needed for the data cursor text for any axes.
S.index = index;
% Define the update function
dcm.UpdateFcn = @(empty,info,data)dcmUpdateFcn(empty,info,S);
function txt = dcmUpdateFcn(~, info, S)
% Identify subplot tag
subplotTag = info.Target.Parent.Tag;
% Create text output based on which subplot was identified
switch subplotTag
case 'subplot1'
% Display line name
txt = sprintf('Line Name: %s', info.Target.DisplayName);
case 'subplot2'
% Find closest point to mouse click
d = pdist2([info.Target.XData(:), info.Target.YData(:)], info.Position);
[~, minIdx] = min(d);
% Display the x value, y value, and index value
txt = sprintf('X = %.3f\nY = %.2f\ni = %d', ...
info.Target.XData(minIdx), info.Target.YData(minIdx), S.index(minIdx));
otherwise
% Define a default output for axes that aren't identified
txt = 'Undefined';
end
end
Example of result:
  16 Kommentare
Adam Danz
Adam Danz am 20 Apr. 2020
dataTipTextRow was released in r2019a, actually.
The datacursormode is set as a figure property. If you're applying it to a different figure, you need to set it for that figure.
Try setting the datacursormode for the new figure,
dcm2 = datacursormode(fig2);
dcm2.Enable = 'on';
and then assigning the existing update function,
dcm2.UpdateFcn = @(empty,info,data)dcmUpdateFcn(empty,info,S);
Pedro Augusto de Castro e Castro
Yes, I could make it work using scatter3. My problem is doing this with a surface plot, because I have 4 vectors that I want to be displayed, the X Y Z and, for each point, a info from anothar vector, F.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Performance finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by