matlab uicontrol using figure

4 views (last 30 days)
zhi zhu
zhi zhu on 20 Mar 2017
Edited: 伟民 苑 on 12 Jun 2022
So I have this code written, works completely but I just want to know are there's anyway to implement it so it's shorter, like using a set function or using a for loop?
val1 = ('csdrkjvnscklenkvt');
val2 = 2;
val3 = 3;
val4 = 4;
val5 = 5;
val6 = 6;
val7 = 7;
val8 = 8;
val9 = 9;
val10 = 10;
a = uicontrol('Style','pushbutton','Style','edit','String',val1);
pos = a.Position;
a.Position = [10 400 150 15];
b = uicontrol('Style','pushbutton','Style','edit','String',val2);
pos = b.Position;
b.Position = [10 370 50 15];
c = uicontrol('Style','pushbutton','Style','edit','String',val3);
pos = c.Position;
c.Position = [10 340 50 15];
d = uicontrol('Style','pushbutton','Style','edit','String',val4);
pos = d.Position;
d.Position = [10 310 50 15];
e = uicontrol('Style','pushbutton','Style','edit','String',val5);
pos = e.Position;
e.Position = [10 280 50 15];
f = uicontrol('Style','pushbutton','Style','edit','String',val6);
pos = f.Position;
f.Position = [10 250 50 15];
g = uicontrol('Style','pushbutton','Style','edit','String',val7);
pos = g.Position;
g.Position = [10 220 50 15];
h = uicontrol('Style','pushbutton','Style','edit','String',val8);
pos = h.Position;
h.Position = [10 190 50 15];
i = uicontrol('Style','pushbutton','Style','edit','String',val9);
pos = i.Position;
i.Position = [10 160 50 15];
j = uicontrol('Style','pushbutton','Style','edit','String',val10);
pos = j.Position;
j.Position = [10 130 50 15];
q = uicontrol('Style','pushbutton','Style','text','String','Quit');
set(q,'FontSize',15);
pos = q.Position;
q.Position = [500 20 50 20];
q.Enable = 'Inactive';
q.ButtonDownFcn = 'close(figure(1))';

Accepted Answer

Walter Roberson
Walter Roberson on 20 Mar 2017
vals = {'csdrkjvnscklenkvt' 2 3 4 5 6 7 8 9 10 'Quit'};
nvals = length(vals);
buts = gobjects(1, nvals);
for K = 1 : nvals
buts(K) = uicontrol('Style', 'pushbutton', 'Style', 'edit', 'String', vals{K}, 'Position', [10, 430-30*K, 150, 15]);
end
  2 Comments
zhi zhu
zhi zhu on 20 Mar 2017
the 'Quit' was meant to be a quit button exit when you click on. I fixed that and the program works nicely. The only thing I don't get is the gobjects function. But thank you for your help!
Walter Roberson
Walter Roberson on 20 Mar 2017
gobjects creates placeholders to hold graphics objects.
Note: it is not possible to have a single uicontrol which is two different Style -- if it is pushbutton then it cannot be edit, for example.

Sign in to comment.

More Answers (1)

ES
ES on 20 Mar 2017
You can have a for loop around an uicontrol
Values = {'csdrkjvnscklenkvt', '1', '2', '3'...and so on till 9};
PositionArray = [400:-30:130]
for iloop=1:length(Values)
pushbuttopn(iloop) = uicontrol('Style','pushbutton','Style','edit','String',val1);
pushbutton(iloop).Position = [10, PositionArray(iloop), 50,15];
end
q = uicontrol('Style','pushbutton','Style','text','String','Quit');
set(q,'FontSize',15);
pos = q.Position;
q.Position = [500 20 50 20];
q.Enable = 'Inactive';
q.ButtonDownFcn = 'close(figure(1))';
  2 Comments
zhi zhu
zhi zhu on 20 Mar 2017
I fixed up the code and I'm having problem displaying the 10th value, returning an error message while running second pushbutton in the for loop.
Values = {'csdrkjvnscklenkvt', '1', '2', '3','4','5','6','7','8','9','10'};
PositionArray = 400:-30:130;
for i=1:length(Values)
pushbutton(i) = uicontrol('Style','pushbutton','Style','edit','String',Values{i});
pushbutton(i).Position = [10, PositionArray(i), 50,15];
end
q = uicontrol('Style','pushbutton','Style','text','String','Quit');
set(q,'FontSize',15);
pos = q.Position;
q.Position = [500 20 50 20];
q.Enable = 'Inactive';
q.ButtonDownFcn = 'close(figure(1))';
伟民 苑
伟民 苑 on 12 Jun 2022
Edited: 伟民 苑 on 12 Jun 2022
Values = {'csdrkjvnscklenkvt', '1', '2', '3','4','5','6','7','8','9','10'};
PositionArray = (length(Values).*30+70):-30:70;
for i=1:length(Values)
pushbutton(i) = uicontrol('Style','pushbutton','Style','edit','String',Values{i});
pushbutton(i).Position = [10, PositionArray(i), 50,15];
end
q = uicontrol('Style','pushbutton','Style','text','String','Quit');
set(q,'FontSize',15);
pos = q.Position;
q.Position = [80 PositionArray(6) 50 20];
q.Enable = 'Inactive';
q.ButtonDownFcn = 'close(figure(1))';

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects 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!