Change the value of a variable generated by app designer

조회 수: 21 (최근 30일)
Tim Reddering
Tim Reddering 2022년 5월 24일
댓글: Allen 2022년 5월 31일
Hi,
I use the matlab app designer that creates, in this instance, mutiple numerical edit boxes. In my case this are 30 edit boxes so I wanted to use a for-loop so to not hardcode. All the edit boxes have variable names like editBox_1. I tried to change the value of the edit box with:
for i = 1:30
eval(['editBox_' i '.Value']) = i*50; %in my case the value of editBox_i needed to be i*50
end
This did not change the value of the edit box. Can anyone help me out?
editBox_1.Value = 50 does change the value btw

채택된 답변

Steven Lord
Steven Lord 2022년 5월 24일
Rather than creating 30 variables or 30 properties in the app, I would create a vector that can contain 30 edit box handles and use indexing to address the appropriate edit box handle. While in the example below I'm manually creating the edit box handles in specific positions, you could automate this if you wanted.
f = uifigure;
P = f.Position;
W = P(3)/10; % 10% of the width of the uifigure
H = P(4)/10; % 10% of the height of the uifigure
obj = gobjects(2, 2);
base = [W H 3*W 3*H]; % Base position, lower-left corner
obj(1, 1) = uieditfield(f, 'Value', '(1, 1)', 'Position', base.*[1, 6, 1, 1]);
obj(1, 2) = uieditfield(f, 'Value', '(1, 2)', 'Position', base.*[6, 6, 1, 1]);
obj(2, 1) = uieditfield(f, 'Value', '(2, 1)', 'Position', base);
obj(2, 2) = uieditfield(f, 'Value', '(2, 2)', 'Position', base.*[6, 1, 1, 1]);
With this you could adjust the properties of the edit fields using the elements of obj.
obj(1, 2).BackgroundColor = 'r';

추가 답변 (1개)

Allen
Allen 2022년 5월 24일
Tim,
Without reassigning the edit boxes to a seperate variable, editBox_# in this case, you can use something similar to update values for multiple edit boxes. Note that for this method to work you need to have the main variable constant and be able to edit the property or structure field names. My example uses app as the primary variable.
for i=1:30
app.("editBox_"+i).Value = i*50;
end
  댓글 수: 2
Tim Reddering
Tim Reddering 2022년 5월 30일
This worked. Thank you!
Allen
Allen 2022년 5월 31일
Glad to be of help. Please be sure to accept the answer so that others may also find this post helpful.

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

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by