Obtain data points from plot using 'buttondownfcn' nested functions

function [Xsig,Ysig] = GetPoint(Figure)
set(Figure,'ButtonDownFcn', @ExtractPoint) ;
function ExtractPoint(ClickedPoint,~)
waitforbuttonpress ;
Xsig = get(ClickedPoint,'XData') ;
Ysig = get(ClickedPoint,'YData') ;
end
end
I have a plot created in 'Figure'. I would like to be able to select a variety of points on the curve and export the coordinates into the workspace. I have seen people use ginput and datacursor mode in other forums but neither of these methods work since I have a 2 subplots.
Any help is appreciated. Thank you!

 Accepted Answer

Here is an example
function main
x = rand(100,1); % generate random data
y = rand(100,1);
h = plot(x,y,'.r'); % plot data
set([h gcf],'hittest','off') % turn off hittest
set(gca,'buttondownfcn',@func) % assign function to gca
function func(hobj,~)
p = get(hobj,'currentpoint'); % get coordinates of click
d = pdist2([x y],p([1 3])); % find combination of distances
[~,ix] = min(d); % find smallest distance
line(x(ix),y(ix),'linestyle','none','marker','o')
[x(ix),y(ix)]
end
end

5 Comments

Thank you for this! It's starting to make sense. I assume that lines '% find combination of distances' and '% find smallest distance' are performing calculations for a different problem than just obtaining the point coordinates correct?
Also, this function is in my script at the bottom and I would like to pass the 'p' variable into my script workspace. main should have access to 'p', so how do I make it so that when I call main in my script, it outputs variable 'p'?
p = main() ;
  • I assume that lines '% find combination of distances' and '% find smallest distance' are performing calculations f
Yes, correct
  • my script workspace. main should have access to 'p'
just declare p variable outside the function
darova, I think you misinterpreted my question. main is a local function in my script. main is called by a callbackfcn. I would like to pass variable p from my callbackfcn main into my script workspace. I defined p = 0 in my script but it still does not get passed to my workspace. How can I solve this problem other than using globals?
Try to pass data into UserData property
set(gca,'userdata',num2str(p)) % add this line insdie the function
To get data back
get(gca,'userdata')
% get(gca)
Hello everyone,
I'm also struggling with this. Where should the function (func) be placed in the code view? I have the similar implementation but my click function doesnt seem to be triggered when I click on the plot. I think the reason is becuase it is in another slider function. So I'm not sure now where to place in my click function in the code view.
% Value changed function: FrequencySlider
function FrequencySliderValueChanged(app, event)
value = app.FrequencySlider.Value;
freq=value;
[d,ix]=min(abs(app.freq_vec-freq));
scatter3(app.UIAxes,app.XYZ(:,1)*10000,app.XYZ(:,2)*10000,abs(app.y(:,ix))*10000,[],abs(app.y(:,ix))*10000,'filled','ButtonDownFcn',@click)
view(app.UIAxes, [0 90]);
c = colorbar(app.UIAxes)
colormap(app.UIAxes, jet)
axis(app.UIAxes, 'tight')
axis(app.UIAxes, 'equal')
app.EditField.Value=value;
function click(~,eventData)
coords = eventData.IntersectionPoint;
app.ZEditField.Value=coords;
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Asked:

on 1 Jul 2020

Commented:

on 7 Jun 2023

Community Treasure Hunt

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

Start Hunting!