필터 지우기
필터 지우기

GUIDATA save handles only after exiting callback

조회 수: 3 (최근 30일)
David Nguyen
David Nguyen 2014년 3월 20일
답변: David Nguyen 2014년 3월 20일
Dear All,
I am currently working on a GUI using Matlab GUIDE.
I work with the handles to store some data to be available for all callbacks.
But here comes my problem.
When I introduce additionnal functions into the different callbacks and if those functions try to add specific data to the handles, I have a feeling that the handles is saved only after the callback terminates.
Here is my concrete example:
% Button Callback
function button1_Callback(hObject, eventdata, handles)
% Call specific function
myFunction(handles);
% Use specific data
myOperation = handles.myData + 1;
% Specific function
function myFunction(handles)
% Store data in handles
handles.myData = 1;
% Save handles
guidata(gcbo, handles);
If I proceed this way, I obtain the following error:
Reference to non-existent field 'myData'.
I partially solved the problem by waiting the callback to be closed. And then myData is indeed available for further processing.
My question is therefore the following:
Am I doing the data storage and save right? Is there a way to make myData available already inside the callback (as it is done in my example)?
I hope I was clear in the definition of my problem, in any case I can provide further information upon request.
Thank you very much for your help and have a nice day.
David N.

채택된 답변

David Nguyen
David Nguyen 2014년 3월 20일
Here is a possible solution:
% Button Callback
function button1_Callback(hObject, eventdata, handles)
% Call specific function
handles = myFunction(handles);
% Use specific data
myOperation = handles.myData + 1;
% Specific function
function [newHandles] = myFunction(handles)
% Store data in handles
handles.myData = 1;
% Save handles
newHandles = handles;
Maybe gcbf could give also some results. I let you try^^

추가 답변 (3개)

nl2605
nl2605 2014년 3월 20일
편집: nl2605 2014년 3월 20일
You are doing it alright. The problem is when you store your handles structure. You use gcbo. So it stores only for that callback. Instead store it in hObject or whatever your handles structure is. In your myFunction write guidata(hObject,handles) rather than using gcbo.

David Nguyen
David Nguyen 2014년 3월 20일
Dear nl2605,
I changed my code to:
% Button Callback
function button1_Callback(hObject, eventdata, handles)
% Call specific function
myFunction(hObject, handles);
% Use specific data
myOperation = handles.myData + 1;
% Specific function
function myFunction(hObject, handles)
% Store data in handles
handles.myData = 1;
% Save handles
guidata(hObject, handles);
But the problem remains the same. Maybe I misunderstand something.
Thanks for your answer and take care.
David N.
  댓글 수: 1
nl2605
nl2605 2014년 3월 20일
Its still giving the same error? That's strange. Its working fine for me.

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


David Nguyen
David Nguyen 2014년 3월 20일
Dear nl2605,
I am really surprised that you get the code running.
I checked twice and I still have the same error:
Reference to non-existent field 'myData'.
Unless I wait for the callback to finish.
Do you have any idea where it might comes from?
Thanks and regards.
David N.
  댓글 수: 2
nl2605
nl2605 2014년 3월 20일
Hmmm...I did something that produces the result. But I am not quite sure if that's the right way to do it. Make handles(the structure) as the output argument of your function 'myFunction'. And then when you call this function, call it like this: handles = myFunction(hObject,handles).
David Nguyen
David Nguyen 2014년 3월 20일
This is what I did also to solve the problem. I also feel that it is not optimal but... at least it works^^
Many thanks again nl2605. I will write a formal solution for other readers.
Cya
David N.

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

카테고리

Help CenterFile Exchange에서 Call Web Services from MATLAB Using HTTP에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by