matlab uicontrol using figure

조회 수: 15 (최근 30일)
zhi zhu
zhi zhu 2017년 3월 20일
편집: 伟民 苑 2022년 6월 12일
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))';

채택된 답변

Walter Roberson
Walter Roberson 2017년 3월 20일
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
zhi zhu
zhi zhu 2017년 3월 20일
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 2017년 3월 20일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

ES
ES 2017년 3월 20일
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
zhi zhu
zhi zhu 2017년 3월 20일
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))';
伟民 苑
伟民 苑 2022년 6월 12일
편집: 伟民 苑 2022년 6월 12일
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))';

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by