Question

I'm talking about getting the y-axis value of a point from the plot automatically or from code.

For example I have this point on plot:

enter image description here

If I am only interested in its y-axis I can use this code to get the y-axis value of this point back to my code:

dcm_obj = datacursormode(figure(1));
set(dcm_obj,'DisplayStyle','datatip',...
'SnapToDataVertex','off','Enable','on');
pause();
c_info = getCursorInfo(dcm_obj);

But the problem with this is that I have to manually intervene, use the datacursormode, and click on the point. Only then will the pixel be read and its y-value understood.

Is there anyway I can get this automatically? I mean the actual y-value from the plot without me intervening?

Was it helpful?

Solution

>> h = plot((1:5).^2); %// example plot
>> get(h,'YData')
ans =
     1     4     9    16    25

Or, if the axis only has that one plot, you can get its handle dynamically:

>> plot((1:5).^2); %// example plot
>> get(get(gca,'Children'),'YData')
ans =
     1     4     9    16    25
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top