MATLAB: Save value from Edit Text box from GUI in a text file and on start load value

save value from edit text box from gui in a text file and on start load value

I have a edit text box . When i run the program and write in edit1 box the value eg. 513 number, with a button i must save to a text file the 513 value (eg. data.txt), and next time when i run the program the function must read 513 from data.txt and put the read value from text file in edit text box. My code is:
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @gui_OpeningFcn, ...
'gui_OutputFcn', @gui_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function gui_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = gui_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function varargout = gui(varargin)
function edit1_Callback(hObject, eventdata, handles)
function edit1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
}

Best Answer

By your code we find out that you are using GUIDE but that code is nothing, you just used GUIDE and put one textbox in your GUI, lets do that by parts:
Read the file if it does exist and put the number on the textbox, this is the code that you add to the OpeningFcn
fid = fopen('GUIDATA.txt','r');
if fid~=-1 %if the file doesn't exist ignore the reading code
set(handles.edit1,'String',fscanf(fid,'%c'))
fclose(fid);
end
Execute a function when you press a button. In the callback of the button, easy way from GUIDE layout, click on the button with the right button of your mouse, select View Callback and Callback, that will take you to the right place to put the code
fid = fopen('GUIDATA.txt','wt');
fprintf(fid,'%s',get(handles.edit1,'String'))
fclose(fid);
Another way to save GUI states and probably better, Doug Hull video tutorial: How to save and restore state of a GUI in MATLAB