'Global' Variables in a guide GUI
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
I have a GUI guide function, with multiple buttons. I want each button to change a 'global' variable to a different value. How do I go about declaring the variable correctly, and assigning the values correctly?
채택된 답변
Using global variables is a bad idea in general. They impede the debugging and the increase the level of complexity. You find many corresponding discussions here in the forum, but this problem concens other programming languages also. Ask an internet search engine for details.
But of course it is possible:
- Define the global variables in each function, which needs to access them:
function y = myComputations(x)
global gParameter
y = gParameter * x;
end
- This is done inside the callbacks also:
function Button1_Callback(hObject, EventData, handles)
global gParameter
gParameter = 1;
end
function Button2_Callback(hObject, EventData, handles)
global gParameter
gParameter = 2;
end
- If you need the variable in so base workspace, define them as global here also.
If you need the variable in the base workspace only, e.g. as input for a Simulink method, do not define it as global but using:
function Button1_Callback(hObject, EventData, handles)
assignin('base', 'gParameter' 1);
end
This is not as evil as global variables, but you cannot trace the source of the current value of these variables reliably also.
If 'global' means, that the variable needs to be shared between callbacks of a figure only, use either set/getappdata, guidata or the 'UserData' of the corrsponding GUI element. This allows e.g. to run two instances of the same GUI without conflicts. Please ask, if you need further explanations for this.
댓글 수: 6
Marcus Blackburn
2017년 3월 30일
I don't need the variable to be global, I just need it to go between callbacks of the different pushbuttons. Can you give a little more detail on how to do that?
@Marcus Blackburn: MATLAB has documentation which is free to read online:
Personally I like to use nested functions to pass data between callbacks: neat, intuitive, easy. You will also find one thousand threads on this forum discussing how to pass data between callbacks. Your favorite internet search engine can search this forum and the MATLAB documentation.
sonR
2018년 12월 19일
It was stated in another forum (not sure where) and in the matlab help files that using nested callback functions to share variable data was not recommended. Can anyone elaborate on why? It seems that using the set/getappdata, guidata format makes the variables long and, to me, adds a little more confusion.
@sonny revell: that advice applies to GUIs written using GUIDE, but it does not apply to GUIs written from scratch. The advice can be found here in the MATLAB documentation:
The documentation does not explain why GUIDE is unable to use nested functions.
Personally I always code my own GUIs from scratch, and I prefer to use a combination of local and nested functions within my GUIs: they are simpler, more intuitive, and much easier to use than any other ways of transferring data between callbacks (and the main calling function).
You can find an example here:
So far I have not found any reason to recommend GUIDE to anyone.
Gopinath Karuppannan
2020년 10월 30일
How can we alter the color of each string of gparameter? especially it should be different colors
Rik
2020년 10월 30일
Do you mean in the editor? Or do you mean the values of that variable?
It also sounds like you missed the advice to avoid global variables.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
태그
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
