필터 지우기
필터 지우기

Is there a way to refer to elements in Matlab App using variables?

조회 수: 1 (최근 30일)
Isaac
Isaac 2024년 3월 8일
답변: Walter Roberson 2024년 3월 8일
I have the following section of code, and I wanted to try and shorten it significantly using for loops, but I haven't found a way:
app.Button_1_1.Text = app.images(app.grid(1,1),1);
app.Button_1_2.Text = app.images(app.grid(1,2),1);
app.Button_1_3.Text = app.images(app.grid(1,3),1);
app.Button_2_1.Text = app.images(app.grid(2,1),1);
app.Button_2_2.Text = app.images(app.grid(2,2),1);
app.Button_2_3.Text = app.images(app.grid(2,3),1);
This continues on for around hundred lines to get every button that I'm using. I was thinking of doing something like as follows, but I haven't found anything that works:
for r = 1:3
for c = 1:3
app.Button_r_c.Text = app.images(app.grid(r,c),1);
end
end
Is there a way to do this, or another way to shorten the process? Thanks in advance

채택된 답변

Voss
Voss 2024년 3월 8일
for r = 1:3
  for c = 1:3
      app.(sprintf('Button_%d_%d',r,c)).Text = app.images(app.grid(r,c),1);
  end
end

추가 답변 (1개)

Walter Roberson
Walter Roberson 2024년 3월 8일
for r = 1 : 3
for c = 1 : 3
app.Button(r,c).Text = app.images(app.grid(r,c),1);
end
end

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by