Why is my GUI Slider dissapearing when I set it's position?!

2 次查看(过去 30 天)
This is a very frustrating little problem!!
I'm constructing a GUI, and have decided I want to do it programtically, rather than with GUIDE, so as to have more control.
The interface has 4 axes and a slider. It will soon have more objects, so I've left the bottom 30% of the figure clear (Units Normalized = 0.3).
Attached is the code, which: when I run it (I'm using 2011b) the slider is not shown. Annoyingly, though, if I then execute the last line from the command window, it appears where I want it to!! So I've added the same line in immediately after itself and....low and behold the code works!!
This seems a bit clunky and messy....and I'd like to know what's going on and why this is happening.
Anyone any ideas as to why this is going on??
Regards in advance,
Gav
clear all;
close all;
clc;
%%DEVELOP GUI FIGURE INTERFACE
%%Interface Layout Geometry
f1p=[7,19,1233,954]; %Figure Position Vector
b=30; %Percentage of bottom not consumed by axes
p=3; %Gap size between axes and between edges & axes
wa=(100-3*p)/2; %width of axes
ha=(100-b-3*p)/2; %height of axes
a1p=[p,100-(p+ha),wa,ha]/100; %Axes 1 position vector
a2p=[50+p/2,100-(p+ha),wa,ha]/100; %Axes 2 " "
a3p=[p,b+p,wa,ha]/100; %Axes 3 " "
a4p=[50+p/2,b+p,wa,ha]/100; %Axes 4 " "
s1p=[50+p/2+wa/4,b-p,wa/2,1.5]/100; %Slider 1 Position Vector
%%Generate Objects and Position Accordingly
f1=figure(); %Figure 1
set(f1,'Position',f1p); %Position
a1=axes(); %Axes 1-4
a2=axes();
a3=axes();
a4=axes();
set(a1,'Position',a1p,'Units','Normalized'); %Position Axes
set(a2,'Position',a2p,'Units','Normalized');
set(a3,'Position',a3p,'Units','Normalized');
set(a4,'Position',a4p,'Units','Normalized');
s1=uicontrol(f1,'Style','Slider'); %Slider 1
set(s1,'Position',s1p,'Units','Normalized'); %Position Slider

采纳的回答

Matt Fig
Matt Fig 2012-10-23
编辑:Matt Fig 2012-10-23
Actually it is there, at the default position. The usual way of initializing a uicontrol is to pass all properties at once:
s1=uicontrol(f1,'Style','Slider',...
'Units','Normalized',...
'Position',s1p,);
If you are going to be programmatically making GUIs, you might want to check out these 41 examples that cover most of the basics and some advanced topics too.
  2 个评论
Gavin
Gavin 2012-10-23
Okay, but when I run it in debug it's in the default position (bottom left corner of f1) before I run the line
set(s1,'Position',s1p,'Units','Normalized')
When I run it the first time it disappears....run it again and it re-appears. Have rectified code to position accordingly at object creation (as per your suggestion), but I just can't get my head round why that didn't work? I thought both methods would be valid?
Thanks again for your help!! Gav
Matt Fig
Matt Fig 2012-10-23
I bet what you are seeing is that you first pass the position, then tell it you want the units in normalized coords. Try reversing this order and see if the problem goes away.
set(s1,'Units','Normalized','Position',s1p) % Set units first!
So what is happening is that the default units of uicontrols are pixels. When you tell it the position, it thinks pixels. Then it sees you want normalized units so it converts the current position to read in those coordinates without moving it.

请先登录,再进行评论。

更多回答(0 个)

类别

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