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 comentarios

Ajay Kumar
Ajay Kumar el 14 de Nov. de 2019
Editada: Ajay Kumar el 14 de Nov. de 2019
Do you mean line on the axes in uifigure?
John D
John D el 14 de Nov. de 2019
Yes

Iniciar sesión para comentar.

 Respuesta aceptada

Adam Danz
Adam Danz el 14 de Nov. de 2019
Editada: Adam Danz el 14 de Nov. de 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 comentarios

John D
John D el 14 de Nov. de 2019
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 el 14 de Nov. de 2019
Editada: Adam Danz el 14 de Nov. de 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?
John D
John D el 14 de Nov. de 2019
I'm using version 2019a
Adam Danz
Adam Danz el 14 de Nov. de 2019
The first question was more important.
John D
John D el 15 de Nov. de 2019
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.
Adam Danz
Adam Danz el 15 de Nov. de 2019
Maybe it's a version issue. I have r2019a available and can test this in a couple of hours and get back to you.
John D
John D el 15 de Nov. de 2019
Thanks
Adam Danz
Adam Danz el 15 de Nov. de 2019
Editada: Adam Danz el 15 de Nov. de 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?
John D
John D el 15 de Nov. de 2019
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 el 15 de Nov. de 2019
Editada: Adam Danz el 15 de Nov. de 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
John D
John D el 15 de Nov. de 2019
Ok, thanks for your help.
Would this functionality be available in GUIDE, out of interest?
Adam Danz
Adam Danz el 16 de Nov. de 2019
I'm not sure which function you're asking about. drawline() should work on regular axes used by guide GUIs.
John D
John D el 16 de Nov. de 2019
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!
Image Analyst
Image Analyst el 16 de Nov. de 2019
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.
John D
John D el 16 de Nov. de 2019
Editada: John D el 16 de Nov. de 2019
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);
Adam Danz
Adam Danz el 16 de Nov. de 2019
Ok, good! If you start using a more recent release you could implement it in app designer, too (r2019b or later).

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Interactive Control and Callbacks en Centro de ayuda y File Exchange.

Productos

Versión

R2019a

Preguntada:

el 14 de Nov. de 2019

Comentada:

el 16 de Nov. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by