Generalizing Function Calls with Property Dependence in App Designer

2 ビュー (過去 30 日間)
etia
etia 2023 年 4 月 18 日
コメント済み: etia 2023 年 4 月 19 日
I have code that is formatted like below in App Designer. Properties I1, I2, and I3 are all callable from an instrument with their own method. So far I've just been repeating the same code in each button push callback, but I want to write a general function instead so I don't repeat code. The example below is a simple one for this purpose, but you could imagine how convoluted it could get for several methods. I understand why the code below doesn't work (it's calling a string, not the property). I am unclear as to how to generalize a format of a property and call it in the function. Help is appreciated.
Version 2020b. An upgrade is possible, if that assists with the solution.
%this is the start of App Designer
properties (Access = private)
I1
I2
I3
end
methods (Access = private)
function change_current(app, unit_number)
prop_name = join(["I", unit], '')
app.prop_name.current(40) %the code errors here as it should, since it's calling a string and not the property...how to do?
end
end

採用された回答

chicken vector
chicken vector 2023 年 4 月 19 日
編集済み: chicken vector 2023 年 4 月 19 日
Use:
app.(prop_name).current
to use strings as class properties (or structure fields).

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDevelop 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