MATLAB Answers

How do i select a point on matlab UIaxes and then get data for it?

조회 수: 216(최근 30일)
I have a plot on UIAxes and i want to use the brush tool to select the already plotted data and get what points the user selects

  댓글 수: 0

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 17 Feb 2020
편집: Adam Danz 28 Sep 2020
The brush tool is not (yet?) available on UIAxes as of r2019b; neither is rbbox. Update: the brush tool is now available as of r2020a.
Here's a workaround for releases <= r2019b.
The first block of code simulates an App by creating a plot on a UIAxes within a UIFigure.
The second block of code is a function that can be added to your App. When the function is called, the title of the axes will change to instructions for the user to draw a rectangle with their mouse. After drawing the rectangle, it will disappear and the (x,y) coordinates within or on the border of the rectangle will be returned.
See inline comments for details.
% Create a demo-app
app.myApp = uifigure();
app.UIAxes = uiaxes(app.myApp);
plot(app.UIAxes, rand(10,4),'o')
title(app.UIAxes, 'My Data')
% Call the custom function so the user can
% draw a temporary rectangle within the axes.
[x,y] = selectDatapoints(app.UIAxes);
function [x,y] = selectDatapoints(ax)
% Remove any pre-existing rectanlges (if any)
delete(findall(ax, 'Type', 'images.roi.Rectangle'))
% Get coordinates of all data points in the axes
xyobj = findall(ax.Children, '-Property','xData');
xydata = get(xyobj, {'XData','YData'});
xydataMat = cell2mat(xydata');
% change title of axes to instructions, in red
originalTitle = get(ax.Title, {'String', 'Color'});
set(ax.Title, 'String', 'Draw rectangle around desired datapoints', 'Color', 'r')
% allow user to draw rectangle; see more options:
pan(ax, 'off') %turn off panning so the interaction doesn't drag the data.
roi = drawrectangle(ax);
% quit if figure is closed
if ~isvalid(roi)
x = [];
y = [];
% Return original title
set(ax.Title, 'String', originalTitle{1}, 'Color', originalTitle{2})
% determine which coordinates are within the ROI
isIn = xydataMat(1,:) >= roi.Position(1) & xydataMat(1,:) <= sum(roi.Position([1,3])) ...
& xydataMat(2,:) >= roi.Position(2) & xydataMat(2,:) <= sum(roi.Position([2,4]));
% Delete ROI
% Return outputs
x = xydataMat(1,isIn);
y = xydataMat(2,isIn);

  댓글 수: 22

표시 이전 댓글 수: 19
Adam Danz
Adam Danz 19 Feb 2020
As you can see in the comments following that answer, the solution worked for at least two people using r2019b (including me). If you're having trouble implementing the solution I wonder what troubleshooting you've already done to try to get it working.
"it says my tth is an empty array when i run the first line of code"
That's fine and is not a problem. That just means there were no pre-existing datatips.
Peter Harrington
Peter Harrington 26 Sep 2020
It is a nice solution. The pan function call should have ax and not app.UIAxes. IT IS IMPORTANT TO NOT THAT THIS SOLUTION REQUIRES THE IMAGE PROCESSING TOOBOX.
Adam Danz
Adam Danz 28 Sep 2020
Thanks, Peter. I corrected the pan(app.UIAxes...) error.

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Cameron B
Cameron B 5 Jul 2020
Looks like there's something easier to do in 2020a. May want to try it out in 2019a or 2019b as I did not see it in the current documentation, but it seems to work fine for me.
%app.UIAxes will be our handle
x = 1:10; %some values for x
y = x*2; %some values for y
plot(app.UIAxes,x,y) %plot in the UIAxes
indx = app.UIAxes.Children(end).BrushData; %grab the indices

  댓글 수: 2

Adam Danz
Adam Danz 6 Jul 2020
This can be activated by selecting the brush icon that appears in the tools menu when hovering over the axes. The BrushData referenced in your answer only returns the index of selected items of a single object after using the brush tool.
Priti Madurwar
Priti Madurwar 26 Oct 2020
How do we get thos values back in command window?
bcz indx returns empty when called in command window.
how do i get those accurate values if i neeed to display them in some other window?

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by