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

20 次查看(过去 30 天)
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

采纳的回答

Cris LaPierre
Cris LaPierre 2021-4-22
编辑:Cris LaPierre 2021-4-22
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 个评论
Bruce Rodenborn
Bruce Rodenborn 2024-1-19
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 2024-1-19
编辑:Cris LaPierre 2024-1-19
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.

请先登录,再进行评论。

更多回答(1 个)

Bruce Rodenborn
Bruce Rodenborn 2024-1-19
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

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by