필터 지우기
필터 지우기

How to update handles structure from a helper function in GUIDE ?

조회 수: 1 (최근 30일)
CY Y
CY Y 2014년 5월 23일
댓글: Peter 2017년 11월 20일
I wrote an image browser using GUIDE (Matlab 2014a). Originally, one of the function callback looks like this, and it worked.
function btnLineProfileXY_Callback(hObject, eventdata, handles)
figure(4);hold off;
imshow(squeeze(handles.FRET(:,:,getappdata(handles.figure1,'Zind'),:)));
hold on;
handles.hL1 = line(getappdata(handles.figure1,'Xind')*[1 1],[1 handles.Ynum]);
handles.hL2 = line([1 handles.Xnum], getappdata(handles.figure1,'Yind'));
lineBtnDownFcn(handles);
guidata(hObject, handles);
But then because many other callbacks use the same codes, so I decided to write a helper function for these repetitive codes.
function btnLineProfileXY_Callback(hObject, eventdata, handles)
figure(4);hold off;
imshow(squeeze(handles.FRET(:,:,getappdata(handles.figure1,'Zind'),:)));
hold on;
lineRoutine(handles);
lineBtnDownFcn(handles);
guidata(hObject, handles);
function lineRoutine(handles) % Helper function
figure(4);hold off;
imshow(squeeze(handles.FRET(:,:,getappdata(handles.figure1,'Zind'),:)));
hold on;
handles.hL1 = line(getappdata(handles.figure1,'Xind')*[1 1],[1 handles.Ynum]);
handles.hL2 = line([1 handles.Xnum], getappdata(handles.figure1,'Yind'));
guidata(handles.figure1, handles);
When structured like this, it doesn't work anymore. The line object handles are not save to the handles structure. The function call was successful and there was no error message generated. But when I examine the handles structure after the function call, there was no handles.hL1 and handles.hL2. What did I do wrong?

채택된 답변

Jan
Jan 2014년 5월 23일
The debugger reveals the reasons for such problems. Set a breakpoint in the code and check, what's going on by stepping through the code line by line.
In lineRoutine the variable "handles" is modified and stored in the figure. The same happens in lineBtnDownFcn. But then the version of "handles" in the function btnLineProfileXY_Callback is stored finally, such that the modifications vanish.
Omit the guidata(hObject, handles) in btnLineProfileXY_Callback to avoid overwriting the changes.
  댓글 수: 1
Joe Yeh
Joe Yeh 2014년 5월 24일
편집: Joe Yeh 2014년 5월 24일
Hi Jan, thanks for the answer. I tried again but it still didn't work. I tested what you said in a very simple GUI that had just an axes and a push button.
function pushbutton1_Callback(hObject, eventdata, handles)
drawline(handles);
handles.test = 2; % In the second test, a stop is placed at this line.
function drawline(handles)
handles.hL1 = line([0 200], [100 100]);
guidata(handles.figure1, handles);
test(handles);
function test(handles)
handles.test = 1; % In the first test, a stop was placed at this line
If the program is stop at 'handles.test=1', I see the handles.hL1. But if the program is stopped at 'handles.test=2', I can not see handles.hL1. Somehow the pushbutton1_Callback is holding on the the handles structure first passed on to it..?
Your solution will work if the helper function is called in the openingFcn of the GUI program. But if helper function is called from a callback, your solution didn't seem to work...

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

추가 답변 (1개)

CY Y
CY Y 2014년 5월 27일
After playing with it for a quite a while, I found a solution to my own problem. Since the problem is that the GUI function callback and the helper function ended up with different version of handles structure, making helper function return its version of handles structure to the callback should solve the problem. It appears that the change guidata is supposed to do during the execution of helper function will be gone once the function call ended, unless helper function is called from the OpeningFcn of the GUI. So I rewrite my code like this.
function btnLineProfileXY_Callback(hObject, eventdata, handles)
figure(4);hold off;
imshow(squeeze(handles.FRET(:,:,getappdata(handles.figure1,'Zind'),:)));
hold on;
handles = lineRoutine(handles);
guidata(hObject, handles);
function handles = lineRoutine(handles) % Helper function
figure(4);hold off;
imshow(squeeze(handles.FRET(:,:,getappdata(handles.figure1,'Zind'),:)));
hold on;
handles.hL1 = line(getappdata(handles.figure1,'Xind')*[1 1],[1 handles.Ynum]);
handles.hL2 = line([1 handles.Xnum], getappdata(handles.figure1,'Yind'));
Indeed, this solved my problem.

카테고리

Help CenterFile Exchange에서 Graphics Objects에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by