Save value from Edit Text box from GUI in a text file and on start load value
2 ビュー (過去 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
}
0 件のコメント
採用された回答
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
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 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!