matlab uicontrol using figure

5 Ansichten (letzte 30 Tage)
zhi zhu
zhi zhu am 20 Mär. 2017
Bearbeitet: 伟民 苑 am 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))';

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 20 Mär. 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 Kommentare
zhi zhu
zhi zhu am 20 Mär. 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 am 20 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

ES
ES am 20 Mär. 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 Kommentare
zhi zhu
zhi zhu am 20 Mär. 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))';
伟民 苑
伟民 苑 am 12 Jun. 2022
Bearbeitet: 伟民 苑 am 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))';

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by