How to intialize a gui panel and make it visible? Why is it different from creating a button? (see example)

조회 수: 1 (최근 30일)
function gui_example
f = figure('Visible','off','Position',[0,0,600,400]);
mybutton = uicontrol('Style','pushbutton','String','Example button','Position',[50,150,500,50]);
mypanel = uipanel('Title','Example panel','Position',[50,250,500,50]);
f.Visible = 'on';
end
Why button is visible, but panel is not? What am I doing wrong?
  댓글 수: 1
Merse Gaspar
Merse Gaspar 2023년 6월 6일
I've tried to use uifigure instead of figure, and the panel turned to be visible, but the appearance of elements (both the button and panel) differ very much from what I expect.

댓글을 달려면 로그인하십시오.

답변 (1개)

Cris LaPierre
Cris LaPierre 2023년 6월 6일
uipanels and uicontrol need to be placed on uifigures. You need to tell it which figure to place the components on. I would try something like this.
f = uifigure('Visible','off','Position',[0,0,600,400]);
mybutton = uicontrol(f,'Style','pushbutton','String','Example button','Position',[50,150,500,50]);
mypanel = uipanel(f,'Title','Example panel','Position',[50,250,500,50]);
f.Visible = 'on';
  댓글 수: 4
Cris LaPierre
Cris LaPierre 2023년 6월 7일
Fair enough. You can find a similar looking example in the documentation as well
f = figure;
p = uipanel(f,'Position',[0.1 0.1 0.35 0.65]);
c = uicontrol(p,'Style','slider');
c.Value = 0.5;
There are two separate ways to build guis in MATLAB. The original approach used m-files and fig files (GUIDE). These are known as figure-based apps. Creating apps this way is no longer recommended, and will be removed in a future release. Note that the instructions you are using are from R2015a, so it is a bit dated.
The new way to create guis is using app designer, which is based on uifigures.
Perhaps a more up-to-date reference might be this page? https://www.mathworks.com/help/matlab/develop-apps-programmatically.html
Also, MathWorks just released a free App Building Onramp on building apps in App Designer.
Because of this difference, the properties of your component can differ based on which type of figure you are placing it on. For example, this text can be found on the uipanel documentation page:
  • "Some properties and property values of Panel objects differ depending on whether the panel is a child of a figure created using the uifigure function or the figure function. The uifigure function is the recommended function to use when building new apps, and is the function used in App Designer apps. For more information, see Ways to Build Apps."
Cris LaPierre
Cris LaPierre 2023년 6월 7일
What version of MATLAB are you using?
Here is what it might look like in App Designer

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by