Calling an app variable from it's string name
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi, I am creating an app. In this app there is a checkbox for every car. There are 10 cars so I changed each box name as
app.CalculatePlmCheckBoxC1
app.CalculatePlmCheckBoxC2
...
And after this I want to assign the value of these boxes to certain variables. My code is below, but it doesn't work. and I can't solve the problem.
for numOfCars = 1:1:10
calculatePlmApp = sprintf('app.CalculatePlmCheckBoxC%.Value',numOfCars);
varValue = eval(calculatePlmApp);
newName = sprintf('PlmC%d',numOfCars);
assignin('base',newName,)varValue;
end
I would reallyappreciate it if you could help. Thank you already!
댓글 수: 2
Stephen23
2021년 12월 28일
편집: Stephen23
2021년 12월 28일
Replace the anti-pattern, evil, obfuscated EVAL-based code with this simpler, efficient code:
F = sprintf('CalculatePlmCheckBoxC%d',numOfCars);
V = app.(F).Value
Note that magically making variables appear in other workspace is slow, complex, and inefficient.
Note that numbering variables like that makes processing your data more complex then if you used basic indexing.
Is there a particular reason why you cannot process that data within the GUI ?
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!