Displaying information about the data set by clicking on its plot and then show a value that is associated with the (x,y) point

Hello,
So, I have a GUI and I'll plot a figure in this GUI. Then I want the user to be able to click somewhere on the curve, and then the information about that point will show up. I followed what was said here: https://www.mathworks.com/matlabcentral/answers/11668-displaying-information-about-the-data-set-by-clicking-on-its-plot
But, also, I want, aside from showing the x and y coordinates, to show another information that's associated with each (x,y) point. Like a (x,y) point indicated the value of z. Is it possible?
For example:
x =
0.0960 0.2496 0.4032 0.5568 0.7104 0.8640
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
y =
0.0960 0.2496 0.4032 0.5568 0.7104 0.8640
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36
fh = figure;
plot(x(2,:), y(2,:)); hold on;
plot(x(3,:), y(3,:)); hold on;
plot(x(4,:), y(4,:)); hold on;
datacursormode on
dcm = datacursormode(gcf);
set(dcm,'UpdateFcn',@myupdatefcn)
function txt = myupdatefcn(trash,event)
pos = get(event,'Position');
dts = get(event.Target,'Tag');
txt = {dts,...
['x: ',num2str(pos(1))],...
['y:', num2str(pos(2))]};
I want to show x(1, ?) and y(1, ?) when the user clicks on a point in the curve.
Thank you all

2 Kommentare

Is the figure external to the GUI (I see you're producing a figure in your code).
If not, what kind of app are you building? AppDesigner? Guide? uicontrol?
The figure is plotted in a GUI axes, once the user press a push button. I'm using GUIDE.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Matt J
Matt J am 17 Jan. 2020
Bearbeitet: Matt J am 17 Jan. 2020
....
set(dcm,'UpdateFcn',@(t,e) myupdatefcn(t,e,x,y) );
function txt = myupdatefcn(~,event,xdata,ydata)
pos = get(event,'Position');
dts = get(event.Target,'Tag');
[~,j]= find( xdata==pos(1) & ydata==pos(2) );
txt = {dts,...
['x: ',num2str(pos(1)),' x(1,?): ', num2str(xdata(1,j))],...
['y: ',num2str(pos(2)),' y(1,?): ', num2str(ydata(1,j))]};
end

6 Kommentare

@Pedro, in addition to these changes, you should also make the following changes.
1) Your code is creating a new figure rather than using your GUI axes. To fix that, specify the axes in all of the graphics functions. This is generally a good practice.
hold(handles.axes1, 'on') % provide axis handle
plot(handles.axes1, x(2,:), y(2,:)); % provide axis handle
plot(handles.axes1, x(3,:), y(3,:)); % provide axis handle
plot(handles.axes1, x(4,:), y(4,:)); % provide axis handle
datacursormode(handles.figure1,'on') % provide figure handle
dcm = datacursormode(handles.figure1); % provide figure handle
set(dcm,'UpdateFcn',@(t,e) myupdatefcn(t,e,x,y) );
2) In the update function, it is assumed that the additional information for each point is stored in the Tag property of each line object. Currently your tag properties are empty which will result in an empty line in your datatip window. So, you'll either need to assign a constant value to the tag property or you'll need to select some other bit of information to display.
% Example:
plot(handles.axes1, x(2,:), y(2,:),'Tag','line1')
Thank you! This works!
@Adam, I plotted the figure using the GUI axes (I'm sorry about the example I've put in the question, it was not clear).
I've put the code inside the ButtonDownFcn related to the GUI axes in question. When I put the handle like this
datacursormode(handles.graph_5, 'on')
it gives me an error.
Thanks about the Tag property!
That should give you an error. If you take a 2nd look at my code, I provided the figure handle to datacursormode, not an axes handle.
I see. So what would be the figure handle? The GUI figure?
For GUIDE GUIs, yes. It's not required, but recommended.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-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