This is a similar problem encountered by many others where guidata(hOjbect, handles)
does not seem to update a value. I'm using it with a listener, and am not sure how to proceed.
In my gui_OpeningFcn
I have the following line:
addlistener(handles.s, 'name', 'PostSet', @(s,e)updatefilesave(hObject, [], handles));
This sets the listener appropriately and it does call updatefilesave
when name is modified. However, inside updatefilesave
is the following code:
handles.fileUnsaved = true;
guidata(hObject, handles);
Inside the function, both lines work. When I breakpoint on the first line and step, fileUnsaved
gets set to true. After I step the second line (while still inside the updatefilesave
function), handles.fileUnsaved
is still set to true.
However, when I step out of the function, the green arrow gets put on to the addlistener
line in the gui_OpeningFcn
function. at this level, handles.fileUnsaved
is now set back to false.
How do I get handles to update when using a listener?
EDIT
What I'm trying to do is know when input fields have changed so I can prompt the user to save their work before closing the program. I check the fileUnsaved flag in the CloseRequestFcn and if it is true, I ask the user if they want to save before exiting.
function namebox_Callback(hObject, eventdata, handles)
newName = handles.namebox.String;
if ~isempty(newName)
handles.s.name = newName; % (listener gets triggered here post set)
end
handles.namebox.String = handles.s.name;
guidata(hObject, handles); % (namebox's local handles with fileUnsaved set to false gets put into hObject)
This is why I cannot call handles = guidata(hObject)
in the CloseRequestFcn
. The only way to stop this is to call handles = guidata(hObject)
in the namebox callback before I call guidata(hObject, handles)
. But doing that everywhere would defeat the point of using listeners. I would just go and set fileUnsaved to true in every callback function (about 50 of them).