declare variable accross all button
조회 수: 1 (최근 30일)
이전 댓글 표시
hello guys, i have problem in declaring a variable, im building a GUI(guide user interface) with some buttons, what i mean is to declare variable "k=1" globally so every buttons "function pushbutton_Callback(hObject, eventdata, handles)" can use it in the same mean, because every time i push other buttons it says "??? Undefined function or variable 'k' " . if u guys have exprience in building gui, where should i put the variable at? or maybe using particular function?
댓글 수: 0
채택된 답변
Image Analyst
2011년 10월 29일
The above two options, plus some additional ones, are described in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.3F
댓글 수: 6
추가 답변 (2개)
Walter Roberson
2011년 10월 29일
It is not possible to do that in MATLAB, and it would be a disaster if you could, since there are no doubt plenty of MATLAB library routines that use "k" for their own purposes.
Ordinarily, each MATLAB function has its own local variables, which are separate from those of other functions, and from those of the base workspace. However, if several functions, and possibly the base workspace, all declare a particular name as global, they all share a single copy of that variable. Any assignment to that variable, in any function, is available to all the functions declaring it global.
Notice how the value is only shared with "all the functions declaring it global", not with all routines.
댓글 수: 0
Naz
2011년 10월 29일
Just simply use handles.k instead of k everywhere. Then, after you changed this variable and want to save changes to it, you need to type
guidata(hObject, handles); % this saves changes
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!