Question

Matlab has the ginput for getting the (x,y) value of where you have clicked on the graph. However, say that I have plotted a graph with plot(t, sin(2*pi*t)). I want to be able to have the cursor move on the actual plot, and by clicking it, I get the (x,y) coordinate of my plot (in this case, the time and the value of sine).

Is this possible?

Was it helpful?

Solution

When the MATLAB figure is open, you can use the data cursor tool to help you traverse through the plot.

Here's a quick example:

t = 0 : 0.01 : 10;
plot(t, sin(t));

Here's what your plot looks like, and I have highlighted the option of where you can click for the data cursor tool.

enter image description here

Once you click on the data cursor option, click anywhere on the plot and it'll give you a data cursor tip. At that point you selected, it shows the x and y co-ordinate. In your case, x denotes time and y denotes amplitude.

enter image description here

Once you have a data cursor tip there, click on it (the black square) and drag it along the curve. You will see that the data cursor tip updates and moves along the curve. Judging from your question, I believe this is what you're looking for.

Take note that the data cursor tip only works for the points that you have plotted. It does not interpolate in between the points. As such, if you want finer resolution, choose a smaller step size between the beginning and end points.

Also, if you want to know the exact index of where your point is, plot just the amplitude values themselves:

plot(sin(t));

The x axis will plot the index number of each point instead of the time values. You can then use the data cursor tip to locate which index you want, then do that indexing that you have specified in your comment above. In other words:

enter image description here

This is telling us that at index 304, it gives us a y value of 0.1114. Then you could do t(304) to find the actual time value where the amplitude was at 0.1114.

OTHER TIPS

h = plot(t, sin(2*pi*t)); %// get a handle to the graph
[x, ~] = ginput(1); %// y from ginput not wanted; it will be read from the graph
y = interp1(get(h,'XData'), get(h,'YData'), x); %// interpolate the graph for y
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top