handles object behaving like it was passed by value

조회 수: 2 (최근 30일)
Jeff
Jeff 2011년 8월 8일
Hi all, I've developed a GUI with 3 simple buttons on it, with tags pb1, pb2, and pb3. I start by calling the GUI .m file, which initializes the handles object. The pb2 and pb3 buttons are initially invisible. When the callback function for pb1 is activated, pb1 is made invisible and pb2 and pb3 visible. Inside the pb1 callback code block another function is called with the handles object as an argument, which runs a psych experiment. Inside the psych experiment after a stimulus is presented, the program waits for a response from the user (using waitforbuttonpress). When the user clicks on pb2, handles.resp is set to 2, and when they click on pb3, handles.resp is set to 3, and the handles object is updated (handles.resp = 3; guidata(hObject, handles)). However, the handles object that was passed as an argument to the psych experiment function is not updated, so handles.resp winds up referring to the incorrect value. This suggests to me that the handles object is not behaving as if it were passed by reference. How do I pass the handles object by reference so its elements update properly?

채택된 답변

Walter Roberson
Walter Roberson 2011년 8월 8일
handles is not passed by reference. If you update it in a subroutine then before you use it, you need to guidata() to get the updated structure.
  댓글 수: 2
Jeff
Jeff 2011년 8월 8일
Thanks Walter. So handles isn't a subclass of the handle class?
The bottom of this documentation page is really confusing.
http://www.mathworks.com/help/techdoc/matlab_oop/brqzfut-1.html#brqzfut-3
Under "Passing Handle Objects", it says when you pass a handle to a function, a copy of it is made referring to the same object. But then under "MATLAB Passes Handles By Value", it says the opposite. I don't get it.
Walter Roberson
Walter Roberson 2011년 8월 8일
The "handles" parameter used by GUIDE, the one fetched or stored by guidata(), is indeed *not* a member of the "handle" class. It is instead a structure that is passed by value. The structure is often used to hold values that _are_ members of the "handle" class, but the collection of handle class members is not itself a handle.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Handle Classes에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by