Borrar filtros
Borrar filtros

Static text with Gui and condition

2 visualizaciones (últimos 30 días)
Julius MANILA
Julius MANILA el 14 de Abr. de 2017
Respondida: Sulaymon Eshkabilov el 25 de Dic. de 2023
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 comentario
Hassaan
Hassaan el 25 de Dic. de 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.

Iniciar sesión para comentar.

Respuestas (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 25 de Dic. de 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

Categorías

Más información sobre Data Import and Export en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by