Dynamically name a TextArea

조회 수: 1 (최근 30일)
Rick
Rick 2025년 4월 2일
댓글: Rick 2025년 4월 3일
My GUI have 81 TextAreas. The names are indexed, e.g. A1TextArea and A2TextArea.
App.A1TextArea.Value = 1 changes A1TextArea to 1
What is the code to change the Nth TextArea programatically? N is a variable
App.A+'n'+TextArea.Value = 1
  댓글 수: 1
Stephen23
Stephen23 2025년 4월 2일
Rather than using pseudo-indices in text, why not use actual indices? Then your task is achieved using basic indexing.

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

채택된 답변

Walter Roberson
Walter Roberson 2025년 4월 2일
편집: Walter Roberson 2025년 4월 2일
for n = 1 : 81
App.("A" + n + "TextArea").Value = 1
end
That said, it is better to create an array of text areas instead of naming them each individually. With an array of text boxes you could do
set(App.TextAreas, 'Value', 1)
to set the Value property of all of the TextAreas at the same time.
  댓글 수: 1
Rick
Rick 2025년 4월 3일
Thanks for the answer! It is working. I see your point. I have to learn how to create an array of text boxes to simplify the code.

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

추가 답변 (1개)

Steven Lord
Steven Lord 2025년 4월 2일
While you can access property names dynamically as stated in the "Reference Properties Using Variables" section of this documentation page:
R = RandStream.getGlobalStream;
R.Type % Accessing a property using a name known when the code is written
ans = 'mt19937ar'
name = 'Type';
R.(name) % Accessing a property using a name that could vary at run-time
ans = 'mt19937ar'
In this case I'd consider using one property that contains an array of handles. With that you can index into an element of the array to access a particular handle.
f = figure(Color = 'r');
textObjectHandles = gobjects(2);
for k = 1:4
textObjectHandles(k) = uicontrol(Style='text', Units='normalized', ...
Position=[0.1 0.2*k 0.5 0.1], String = string(k));
end
textObjectHandles
textObjectHandles =
2x2 UIControl array: UIControl UIControl UIControl UIControl
textbox3 = textObjectHandles(1, 2)
textbox3 =
UIControl (3) with properties: Style: 'text' String: '3' BackgroundColor: [0.9400 0.9400 0.9400] Callback: '' Value: 0 Position: [0.1000 0.6000 0.5000 0.1000] Units: 'normalized' Use GET to show all properties
  댓글 수: 1
Rick
Rick 2025년 4월 3일
I don't think I follow you. I resolved my problem with the first answer though. Thanks for taking the time to answer me. I appreciate.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by