フィルターのクリア

Static text with Gui and condition

2 ビュー (過去 30 日間)
Julius MANILA
Julius MANILA 2017 年 4 月 14 日
回答済み: Sulaymon Eshkabilov 2023 年 12 月 25 日
count = 0; if(R>0) count = count + 1; count = str2num(get(handles.count_red,'string',count)); else count = count + 1; count = str2num(get(handles.count_green,'string',count)); end
should i get my variable count global or local thanks
  1 件のコメント
Hassaan
Hassaan 2023 年 12 月 25 日
In general, global variables should be avoided when possible because they can make the code harder to read and debug due to their accessibility from anywhere in the program. Instead, it is usually better to manage state within the GUI using local variables and handle structures that MATLAB GUIs provide.
In MATLAB's GUI system, data that needs to be shared across different callback functions can be stored in the handles structure, which is passed around by MATLAB's GUI system. This allows for a sort of "local" scope that is limited to the GUI, rather than a truly global variable.

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

回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 12 月 25 日
As @ Muhammad advised, using local variable is more preferable for two reasons - (1) avoid confusion and (2) easy to edit/handle the overall code.
if it necessasry to share the variable within other functions, then it can be used as arguments for the other functions. And the last reserve will be declaring it as a global one.
% Declaring as a local variable
count = 0;
if R > 0
count = count + 1;
set(handles.count_red, 'string', num2str(count));
else
count = count + 1;
set(handles.count_green, 'string', num2str(count));
end
% Declaring as a global variable:
global count;
count = 0;
if R > 0
count = count + 1;
set(handles.count_red, 'string', num2str(count));
else
count = count + 1;
set(handles.count_green, 'string', num2str(count));
end

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by