Calling an app variable from it's string name

1 Ansicht (letzte 30 Tage)
Sinem Erdogan
Sinem Erdogan am 28 Dez. 2021
Kommentiert: Sinem Erdogan am 28 Dez. 2021
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 Kommentare
Stephen23
Stephen23 am 28 Dez. 2021
Bearbeitet: Stephen23 am 28 Dez. 2021
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 ?
Sinem Erdogan
Sinem Erdogan am 28 Dez. 2021
Hi, yes this app is requested from me and I need to create an app. Thank you. There is a syntax that I can't break, they use it in my workplace. So I am kinda forced to work this way. I am new at app designing , I didn't know this type of calling variables.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Workspace Variables and MAT Files finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by