How to write a subfunction inside a GUI function?

2 ビュー (過去 30 日間)
Merse Gaspar
Merse Gaspar 2023 年 5 月 28 日
編集済み: Merse Gaspar 2023 年 5 月 29 日
I Would like to write a subfunction or subscript inside a GUI, but not a callback function, and I want to use any kind of global variables inside this function or subscript, or create new global variables in it. Is it possible?

回答 (1 件)

Walter Roberson
Walter Roberson 2023 年 5 月 28 日
Yes, it is possible.
In the case of App Designer you might have to define the function in a separate .m file if it is not a callback function and is not part of a constructor or method.
If you are using App Designer, it is not clear there is any point in using a global variable instead of a property of the class -- not unless, perhaps, you are working with multiple apps and wanting to communicate between them.
We recommend against using global variables in most cases; they are the slowest form of shared variable, and the more error-prone.
  4 件のコメント
Walter Roberson
Walter Roberson 2023 年 5 月 28 日
When I search that document, I do not find any uses of global . Could you direct me to a specific page in the document that shows using global variables?
Are you looking at section 3, Create a Simple UI Programmatically ? If so then it does not use any global variables.
Merse Gaspar
Merse Gaspar 2023 年 5 月 29 日
編集済み: Merse Gaspar 2023 年 5 月 29 日
I apologise if I did not use the term correctly. In that example, the whole GUI is in a single file (in a main function), and what is defined in the main body of that function is what I call global.
So, as far as I see, it is possible to use variables defined outside a subfunction (with a note about nested functions), but I cannot define new ones inside a subfunction.

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

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by