User`s guide
11 Lay Out a Programmatic GUI
btn1 = uicontrol('Style','pushbutt on',...
'String','Push Me','Position',[37 362 12 7 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('=== Cl ick and drag out a Position rectangle.')
waitforbuttonpress % So that rbbox does not r etu rn immediately
rect = rbbox; % User drags out a rectangl e, releases button
clipboard('copy', rect) % Place set s tring on system
% clipboard as well as returning it
To use getrect:
1 In the editor, place the following statement in your GUI code file to
generate a push button. Specify
getrect within it as the value for the
Position property:
btn1 = uicontrol('Style','pushbutt on','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 displa ys in tha t rectang le.
(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:
btn1 = uicontrol('Style','pushbutt on','String','Push Me',...
'Position',[]);
11-52