How can I use ginput through app designer in MATLAB R2021a?

20 visualizaciones (últimos 30 días)
Hi all,
I am interested in writing a code for a push button in app designer to run ginput to select several points in the 2D data space. My code is here:
% Button pushed function: plot
function plotButtonPushed(app, event)
x = rand(10000,1);
y = rand(10000,1);
plot(app.UIAxes,x,y,'.');
set(app.UIFigure,'CurrentAxes',app.UIAxes);
[x1,y1] = ginput(7);
hold on;
[k1 v1] = convhull(x1,y1);
plot(x1(k1),y1(k1),'c-',x1,y1,'m*','Parent',app.UIAxes);
end
when I run this, it prompts another figure window. I need to run it in the app environment. Would you please correct my code?
Thank you so much in advance,
Moh

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 22 de Abr. de 2021
Editada: Cris LaPierre el 22 de Abr. de 2021
The issue is that, by default, your app's uifigure handle visibility is set to off. Since ginput can't see a figure, it creates one.
In your component browser (right pane), select your main figure (app.UIFigure), then in the inspector, expand Parent/Child and change HandleVisibility to 'on'.
I made some slight changes to your code when testing as well.
% Button pushed function: Button
function ButtonPushed(app, event)
x = rand(10000,1);
y = rand(10000,1);
plot(app.UIAxes,x,y,'.');
[x1,y1] = ginput(7);
hold(app.UIAxes,'on');
[k1 v1] = convhull(x1,y1);
plot(app.UIAxes,x1(k1),y1(k1),'c-',x1,y1,'m*');
hold(app.UIAxes,'off');
end
  3 comentarios
Bruce Rodenborn
Bruce Rodenborn el 19 de En. de 2024
Using 2023b this solution does not work. My axes are called vfieldaxes, but I have otherwise copied and pasted the code. My handlevisibility was also already set to on, so something else is causing the MATLAB figure window to pop up using ginput().
x = rand(10000,1);
y = rand(10000,1);
plot(app.vfieldaxes,x,y,'.');
[x1,y1] = ginput(2);
hold(app.vfieldaxes,'on');
[k1 v1] = convhull(x1,y1);
plot(app.vfieldaxes,x1(k1),y1(k1),'c-',x1,y1,'wo','MarkerSize',16 );
hold(app.vfieldaxes,'off');
Cris LaPierre
Cris LaPierre el 19 de En. de 2024
Editada: Cris LaPierre el 19 de En. de 2024
It works for me in R2023b whether the figure HandleVisibility is 'on' or 'callback'.
Make sure you are setting the canvas (app.UIFigure) handle visibility as shown in the screenshot above.

Iniciar sesión para comentar.

Más respuestas (1)

Bruce Rodenborn
Bruce Rodenborn el 19 de En. de 2024
The solution posted below, on the other hand, does work. The solution in this post states that the handlevisibility state should be "On", when it appears that it should be "CallBack". The correct solution can be found in this post: https://www.mathworks.com/matlabcentral/answers/392617-how-can-i-use-ginput-in-app-designer.
% Set up figure handle visibility, run ginput, and return state
fhv = app.UIFigure.HandleVisibility; % Current status
app.UIFigure.HandleVisibility = 'callback'; % Temp change (or, 'on')
set(0, 'CurrentFigure', app.UIFigure) % Make fig current
[x_r, y_r] = ginput(1);
plot(x_r,y_r,'wo','MarkerFaceColor','w', 'MarkerSize',16)
app.UIFigure.HandleVisibility = fhv; % return original state

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by