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

36 ビュー (過去 30 日間)
Mohammad Shahbazy
Mohammad Shahbazy 2021 年 4 月 22 日
編集済み: Cris LaPierre 2024 年 1 月 19 日
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 Center および File ExchangeVisual Exploration についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by