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

1 visualizzazione (ultimi 30 giorni)
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 Commento
Merse Gaspar
Merse Gaspar il 6 Giu 2023
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.

Accedi per commentare.

Risposte (1)

Cris LaPierre
Cris LaPierre il 6 Giu 2023
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 Commenti
Cris LaPierre
Cris LaPierre il 7 Giu 2023
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."

Accedi per commentare.

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by