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

4 次查看(过去 30 天)
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
}

采纳的回答

Paulo Silva
Paulo Silva 2011-7-4
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
  1 个评论
raviranjan singh
raviranjan singh 2018-5-3
i have save the data of edit text, it not write in same format which i see in editable box.
pls suggest what i change in above code to display in correct format.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by