Refer to field of a struct using input variable

2 ビュー (過去 30 日間)
Ali Ghader
Ali Ghader 2018 年 4 月 19 日
コメント済み: Ali Ghader 2018 年 4 月 19 日

Hello everyone,

Is there a way in matlab to refer to a field of a struct or (any variable) using an input variable ?

Consider I have to functions: Main and Change_Color; and there is a variable app.LampVS.Color that I want to set its value using Change_Color, is there a way to refer to LampVS using a handle for example :

  • Please not that the variable app can be accessed by both functions
 function main(app)
            Change_Lamp(app,'LampVS','green');
 end
 function Change_Lamp(app,Lamp,Color)
            switch Color
                case 'red'
                    Color_Matrix = [1.00 0.00 0.00];
                case 'green'
                    Color_Matrix = [0.04 1.00 0.04];
                case 'orange'
                    Color_Matrix = [1.00 0.65 0.00];
            end
            app. *(@Lamp)?*.Color = Color_Matrix;
   end
  1 件のコメント
Stephen23
Stephen23 2018 年 4 月 19 日
"Is there a way in matlab to refer to a field of a struct ... using an input variable ?"
Of course:

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2018 年 4 月 19 日
app.(Color).Color = Color_Matrix;
  2 件のコメント
Guillaume
Guillaume 2018 年 4 月 19 日
This should of course read
app.(Lamp).Color = Color_Matrix;
Ali Ghader
Ali Ghader 2018 年 4 月 19 日
OMG! XD I searched for half an hour about that and all it needed were two parentheses. Thanks a lot

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by