Clear Filters
Clear Filters

Static text with Gui and condition

2 views (last 30 days)
Julius MANILA
Julius MANILA on 14 Apr 2017
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 Comment
Hassaan
Hassaan on 25 Dec 2023
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.

Sign in to comment.

Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 25 Dec 2023
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

Categories

Find more on Data Import and Export in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!