MATLAB Creating Graphical User Interfaces

(Barry) #1

10 Lay Out a Programmatic UI


TipInstead of using a separate set command, after you decide upon a position for the
object, you can modify the statement in your code file that creates the object to include
the Position parameter and value.

To position components systematically, you can create a function to manage the process.
Here is a simple example function called editpos:

function rect = editpos(handle)
% Enters plot edit mode, pauses to let user manipulate objects,
% then turns the mode off. It does not track what user does.
% User later needs to output a Position property, if changed.

if ~ishghandle(handle)
disp(['=E= gbt_moveobj: Invalid handle: ' inputname(1)])
return
end
plotedit(handle,'on')
disp('=== Select, move and resize object. Use mouse and arrow keys.')
disp('=== When you are finished, press Return to continue.')
pause
rect = handle.Position;
inspect(handle)
To experiment with the function, enter the following code in the Command Window:

hfig = figure;
hsl = uicontrol('Style','slider')
editpos(hsl)
After you call editpos, the following prompt appears:

=== Select, move and resize the object. Use mouse and arrow keys.
=== When you are finished, press Return to continue.
When you first enter plot edit mode, the selection is figure itself. Click the slider to select
it and reposition it. For example, move it to the right side of the figure and orient it
vertically, as shown in the following figure.
Free download pdf