Allow user to draw a line using mouse

How can I get a user to draw a line using their mouse in an App Designer app using R2019a?
Edit: If App Designer does not support it, will GUIDE?
Thanks in advance.

2 Comments

Ajay Kumar
Ajay Kumar on 14 Nov 2019
Edited: Ajay Kumar on 14 Nov 2019
Do you mean line on the axes in uifigure?

Sign in to comment.

 Accepted Answer

Adam Danz
Adam Danz on 14 Nov 2019
Edited: Adam Danz on 14 Nov 2019
If you have the image processing toolbox, use drawline(). Something will have to trigger the function such as a button press or context menu selection. The example below responds to a button press which will then allow the user to click-and-drag a single line on an existing UIAxes.
function ButtonPushed(app, event)
hold(app.UIAxes, 'on')
drawline(app.UIAxes)
% ^^^^^^^^^^ your ui axis handle
end

16 Comments

Thanks for the reply.
However, I get the following error:
Error using images.roi.internal.ROI/parseInputs
ROI parent must be a valid Axes object.
Error in images.roi.Line
Error in drawline (line 158)
h = images.roi.Line(varargin{:});
Adam Danz
Adam Danz on 14 Nov 2019
Edited: Adam Danz on 14 Nov 2019
What handle are you providing to drawline()? As the error indicates, it must be a handles to an axis or UIAxis. What version of matlab are you using?
I'm using version 2019a
The first question was more important.
The handle I am providing is "app.UIAxes" which is the name of an axes control in my app.
I have tried this in MATLAB online and it seems to work, but offline, this error appears.
Maybe it's a version issue. I have r2019a available and can test this in a couple of hours and get back to you.
Adam Danz
Adam Danz on 15 Nov 2019
Edited: Adam Danz on 15 Nov 2019
John, I confirmed that drawline() was not supported for UIAxes in r2019a. It is supported in r2019b. Unfortunately imline() also isn't supported for uiaxes in r2019a (it uses a context menu which doesn't fly with UIAxes).
Option 1 is to use a newer release of Matlab (lots of changes were made to App Designer between 2019a and 2019b so you'd have multiple benefits.
Option 2 is to try to come up with an alternative solution. Could you describe what the purpose of the line will be? Should it always be horizontal or vertical?
Unfortunately, I'm limited to use R2019a.
However, I would like the user to click and drag on the axis to draw a line in a similar way. This would be a straight line drawn in any direction.
Could you suggest a workaround?
Thanks so far for your help.
Adam Danz
Adam Danz on 15 Nov 2019
Edited: Adam Danz on 15 Nov 2019
Hi John, I just toyed around with it for a while. Interactivity with UIAxes in r2019a is really limited. In fact, UIAxes don't even support for callback functions in r2019a. When I tried to add a ButtonDownFcn, an error indicated a lack of support.
Much (if not all) of these features are supported in r2019b. There's probably a creative, albeit messy, solution out there but I've ran out of time for the day.
As a comparison, check out the list of interaction and callback methods available for UIAxes in r2019b
vs r2019a
Ok, thanks for your help.
Would this functionality be available in GUIDE, out of interest?
I'm not sure which function you're asking about. drawline() should work on regular axes used by guide GUIs.
I was asking would the user be able to draw a line with their mouse on an axes in GUIDE, but I've tested it with the same code and it doesn't seem to do anything.
Thank you for your help!
I think drawline() SHOULD work. If it doesn't do anything when the user clicks and drags out a line, then post your code.
And you might want to unaccept the answer until it's solved because many people won't even open your question if they see you've accepted an answer already.
I have tried it again in a new app, and have now got it to work in GUIDE.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
drawline(handles.axes1);
Ok, good! If you start using a more recent release you could implement it in app designer, too (r2019b or later).

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Release

R2019a

Asked:

on 14 Nov 2019

Commented:

on 16 Nov 2019

Community Treasure Hunt

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

Start Hunting!