How can I share data in nested functions of a GUI?

조회 수: 2 (최근 30일)
Mario
Mario 2013년 9월 11일
Hello, I'm not able to share data among nested functions of a GUI using the figure handle, namely the number of clicks on a figure. I'm able to do it only using a global variable. How could I use the handle instead of the global variable? Many thanks! This is my code:
I first define the global variable and then run the function in the workspace:
>>global globalVar >>h = shareData
Then here are the nested functions:
function handle=shareData() fprintf('Starting GUI\n') handle.fig = figure(); global globalVar; globalVar.Nclicks=0; handle.Nclicks=0; fprintf('Before set\n') globalVar handle set(handle.fig,'WindowButtonDownFcn',{@clickPosition,handle})
function handle=clickPosition(hObject, eventdata, handle) global globalVar; get(handle.fig,'CurrentPoint'); globalVar.Nclicks=globalVar.Nclicks+1; handle.Nclicks=handle.Nclicks+1; if globalVar.Nclicks==1 fprintf('After 1 click\n') else fprintf(['After ' , num2str(globalVar.Nclicks), ' clicks\n']) end globalVar handle
  댓글 수: 1
Jan
Jan 2013년 9월 11일
Please follow the "? Help" link to learn how to format code in the forum. Thanks!

댓글을 달려면 로그인하십시오.

답변 (2개)

Jan
Jan 2013년 9월 11일
There are many many threads concerning sharing data between functions of a GUI in this forum. Therefore I suggest to search for "share data gui":
  댓글 수: 2
Mario
Mario 2013년 9월 11일
I should have missed these post before. I'm using the "evalin" and "assignin" functions. Thanks a lot!
Jan
Jan 2013년 9월 12일
EVALIN and ASSIGNIN are a kind of remote control. It is much harder to keep the overview with them and debugging gets extremely hard, because it cannot determined directly, which line of code is responsible for the last changes.

댓글을 달려면 로그인하십시오.


Arthur
Arthur 2013년 9월 11일
evalin and assignin are generally speaking not great ways to do this. Have a look at getappdata & setappdata. Have a look at this video:

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by