why guidata not saving handles data

조회 수: 3 (최근 30일)
James Ang
James Ang 2016년 3월 28일
댓글: Walter Roberson 2017년 8월 29일
Hi,
I've inserted these 2 lines in my GUI opening function:
handles. hText = '';
handles.allLines = [];
set(gcf, ...
'WindowButtonDownFcn', {@clickFcn, handles}, ...
'WindowButtonUpFcn', @unclickFcn);
I've a code that I added 2 data 'allLines' and 'hText'
function plot2_push_Callback(hObject, eventdata, handles)
handles.allLines = findobj(gcf, 'type', 'line');
handles.hText = nan(1, length(handles.allLines));
for id = 1:length(handles.allLines)
handles.hText(id) = text(NaN, NaN, '', ...
'Parent', get(handles.allLines(id), 'Parent'), ...
'BackgroundColor', 'yellow', ...
'Color', get(handles.allLines(id), 'Color'));
end
guidata(hObject, handles);
then I would like to use it here:
function clickFcn(hObject, eventdata, handles)
a = 1;
b = handles.hText;
c = handles.allLines;
disp(a)
disp(b)
disp(c)
but looks like the handles structure does not save the data from the plot callback..
how can I solve this? Thanks.
  댓글 수: 2
James Ang
James Ang 2016년 3월 28일
I realized it'll only update if I set the opening lines into the plot function as below:
function plot2_push_Callback(hObject, eventdata, handles)
handles.allLines = findobj(gcf, 'type', 'line');
handles.hText = nan(1, length(handles.allLines));
for id = 1:length(handles.allLines)
handles.hText(id) = text(NaN, NaN, '', ...
'Parent', get(handles.allLines(id), 'Parent'), ...
'BackgroundColor', 'yellow', ...
'Color', get(handles.allLines(id), 'Color'));
end
guidata(hObject, handles);
set(gcf, ...
'WindowButtonDownFcn', {@clickFcn, handles}, ...
'WindowButtonUpFcn', @unclickFcn);
but why?

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

채택된 답변

Walter Roberson
Walter Roberson 2016년 3월 28일
When you coded
handles. hText = '';
handles.allLines = [];
set(gcf, ...
'WindowButtonDownFcn', {@clickFcn, handles}, ...
'WindowButtonUpFcn', @unclickFcn);
then a copy of the value of the "handles" structure is recorded for use with WindowButtonDownFcn . But that copy has the empty values for those fields, so when you invoke the callback, the fields will be empty in the callback.
You have two choices:
  1. Make "handles" a shared variable (and do not pass it around), so that all changes to "handles" are changing the same variable; or
  2. Pass something into the callback that you can use to locate the master handles structure at the time of the callback
If you are using multiple figures and the handles structure is not attached to the same figure as the WindowButtonDownFcn is acting on, then you need to pass in the figure number that WindowButtonDownFcn is acting on (or you need to pass in some object that exists in that figure.)
If you are using only a single figure, or the handles structure is attached to the same figure that the WindowButtonDownFcn is acting on, then you already have something that can be used to locate handles: namely hObject can be used to locate the master handles structure in such a case:
handles = guidata(hObject);
with no need to pass handles in.

추가 답변 (1개)

Antoine Hurtado Huyssen
Antoine Hurtado Huyssen 2017년 8월 29일
each time you add Something to your handles you need to update the guidata
guidata(hObject,handles);
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 8월 29일
That is not sufficient in this situation. The user had coded
'WindowButtonDownFcn', {@clickFcn, handles}, ...
which causes a copy of the handles structure to be taken and that copy would be what would be passed to clickFcn. Using guidata() to update the master version of the handles structure would not update the copy that was saved.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by