10 Lay Out a Programmatic UI
4 The button Position is set, returned and placed on the system clipboard:
rect =
37 362 127 27
Add a Position parameter and empty value to the uicontrol command from step 1
in your UI code file, as follows:
btn1 = uicontrol('Style','pushbutton',...
'String','Push Me','Position',[])
With the cursor inside the brackets [], type Ctrl+V to paste the setpos output as
the Position parameter value:
btn1 = uicontrol('Style','pushbutton',...
'String','Push Me','Position',[37 362 127 27])
You cannot call setpos when you are creating a component because setpos requires the
handle of the component as an argument. However, you can create a small function that
lets you position a component interactively as you create it. The function waits for you
to press the mouse button, then calls rbbox, and returns a position rectangle when you
release the mouse button:
function rect = getrect
disp('=== Click and drag out a Position rectangle.')
waitforbuttonpress % So that rbbox does not return immediately
rect = rbbox; % User drags out a rectangle, releases button
clipboard('copy', rect) % Place set string on system
% clipboard as well as returning it
To use getrect:
1 In the editor, place the following statement in your UI code file to generate a push
button. Specify getrect within it as the value for the Position property:
btn1 = uicontrol('Style','pushbutton','String','Push Me',...
'Position',getrect);
2 Select the entire statement in the editor and execute it with the F9 key or by right-
clicking and selecting Evaluate Selection.
3 In the figure window, drag out a rectangle for the control to occupy. When you
have finished dragging, the new component displays in that rectangle. (If you type
a character while you are dragging, rbbox aborts, you receive an error, and no
uicontrol is created.)
4 In the editor, select getrect in the uicontrol statement, and type [] in place of it.
The statement now looks like this: