フィルターのクリア

Global variables in GUI

9 ビュー (過去 30 日間)
Jerzy
Jerzy 2017 年 12 月 18 日
コメント済み: Walter Roberson 2017 年 12 月 19 日
Hi everyone, I have a problem with global variable in matlab GUI. I have a main window with options that i want to be active only after setting previous options. That options are placed in menu editor. So I have:
function PREV_OPTION_Callback(hObject, eventdata, handles)
global PREV_OPTION_set;
PREV_OPTION_menu;
if(PREV_OPTION_set)
set (handles.NEXT_OPTION,'enable','on','foregroundcolor',[0 0 0]); % next option is available
end
PREV_OPTION_set is beeing changed in PREV_OPTION_menu to value of 1. While debbuging everything is ok - PREV_OPTION_set equals to 1, but while running the program code in if scope is running since second callback function launch (after second clicking on PREV_OPTION from menu). What is wrong?
  1 件のコメント
Adam
Adam 2017 年 12 月 19 日
It would be a lot easier if you just returned a relevant argument from your PREV_OPTION_menu function. These are the type of errors you get when using global variables.

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

回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 12 月 19 日
If PREV_OPTION_menu is a script that is changing PREV_OPTION_set, and if PREV_OPTION_menu does not itself use "global" in talking about the variable, then the Just In Time compiler might not notice that the variable is changed.
If PREV_OPTION_menu is a function that is changing PREV_OPTION_set and PREV_OPTION_Menu does not use "global" in talking about the variable, then as far as MATLAB is concerned the global variable is not changed.
  2 件のコメント
Jerzy
Jerzy 2017 年 12 月 19 日
PREV_OPTION_menu is a function generated by matlab guide tool, which handles new window. After closing new window (by pushing button - in callback) PREV_OPTION_set variable is set to 1. In callback function for that key i have defined PREV_OPTION_set as global.
Walter Roberson
Walter Roberson 2017 年 12 月 19 日

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

カテゴリ

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