MATLAB: OpeningFcn not saving handles

functionguidatahandlesMATLABopeningsave

I call a callback function in my GUIDE GUI in my OpeningFcn, however I notice that it is not saving the value of handles from the callback function.
The callback function changes the handles value and then calls "guidata" and this appears to work in other places where this function is called. Why is it not saving the handles when the function is placed in my OpeningFcn?
function test_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
pushbutton1_Callback(hObject, eventdata, handles) %not updating
guidata(hObject, handles);

Best Answer

This is occurring because in your OpeningFcn, you are calling "guidata" after calling your callback function.
guidata can manage only one variable at any time. Subsequent calls to guidata(object_handle,data) overwrite the previously stored data.
For this reason, your call to "guidata" after your callback function is overwriting the new handles values. Instead, call "guidata" and then call your callback function.
function test_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
pushbutton1_Callback(hObject, eventdata, handles) %call after guidata so not overwritten