필터 지우기
필터 지우기

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 CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by