App designer - How to index multiple variable names?
조회 수: 2 (최근 30일)
이전 댓글 표시
Hello, i am trying to build a game of Mastermind in matlab app designer.
I have 40 lamps arrainged in 4 columns, and i would like to put them into a cell matrix to referance them more easily to manipulate them. This way i would only have 6 if statements instead of 40 just for changing their color, but i cannot figure out how to do this.
Making a global variable lamps = {lamp_1, lamp_2, lamp_3 etc. or {app.lamp_1, app.lamp_2, etc. doesn't seem to work.
I've tried naming them L101, L102, L201, L202 and trying app.(['L'+num2str('index')]).color = app.white; to no avail.
Anyone know how to do this better?
댓글 수: 0
채택된 답변
Mohammad Sami
2021년 3월 10일
You should add a private property called AllLamps. Then add startupFcn to define this property.
% Code that executes after component creation
function startupFcn(app, varargin)
app.AllLamps = {app.Lamp app.Lamp2 app.Lamp3 app.Lamp4;...
app.Lamp5 app.Lamp6 app.Lamp7 app.Lamp8};
end
The you can use it to change the "Color" property as follows.
function ButtonPushed(app, event)
row = 2;
col = 2;
app.AllLamps{row,col}.Color = 'red';
end
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File 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!