MATLAB: How to set a GUI edit text to clear the first time text is entered

edit textguimatlab gui

I wanted the opening text in my Edit Text button to clear whenever a key was pressed, but never afterwards. I tried to do this using a "FirstEdit" field in handles set to "false" in opening function (after initializing Edit):
function GUI_OpeningFcn(...)
% ...
set(handles.edit1,'String','Initial Text')
handles.FirstEdit = true;
Then, I would modify edit1's keypress to wipe the text field once only:
function edit1_KeyPressFcn(...)
%...
if handles.FirstEdit == true
set(handles.edit1,'String','')
handles.FirstEdit = false;
end
Unfortunately, the FirstEdit variable resets to its initial (OpeningFcn) value after every button press. What am I not getting? Is there a way to implement this?

Best Answer

Did you remember to store the modified handles struct?
function edit1_KeyPressFcn(...)
%...
if handles.FirstEdit == true
set(handles.edit1,'String','')
handles.FirstEdit = false;
end
guidata(hObject, handles)
...
Otherwise the changes of the struct are lost, when the function is left.