Filter löschen
Filter löschen

Static text with Gui and condition

2 Ansichten (letzte 30 Tage)
Julius MANILA
Julius MANILA am 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 Kommentar
Hassaan
Hassaan am 25 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov am 25 Dez. 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

Kategorien

Mehr zu Data Import and Export finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by