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

2 次查看(过去 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."

请先登录,再进行评论。

类别

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