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

2 views (last 30 days)
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

Accepted Answer

Matt Fig
Matt Fig on 23 Oct 2012
Edited: Matt Fig on 23 Oct 2012
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 Comments
Gavin
Gavin on 23 Oct 2012
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 on 23 Oct 2012
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!