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
댓글 수: 0
채택된 답변
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
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
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
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!