MATLAB Answers

0

Save and load edit box input data

Asked by Maximilian Sauter on 18 May 2018
Latest activity Commented on by Rik Wisselink
on 19 May 2018

Hi everybody,

I made a simple gui for example and want to save and load the values in the edit text boxes. In the toolbar of the gui are a save and a load button. In this example, I have 7 edit text boxes. I get the handles, convert the handles into hex, save the new variable and do an easy calculation.

If I close the application I want to load the values I have saved earlier and display the values in the text boxes, so I don't need to fill them in again.

I want to create a kind of a preset that I can load/import for another code I've written.

I hope you get want I need.

Here is my simple code:

Thanks Max

function varargout = example(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @example_OpeningFcn, ...
                   'gui_OutputFcn',  @example_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 example_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = example_OutputFcn(hObject, eventdata, handles) 
varargout{1} = handles.output;
function edit1_Callback(hObject, eventdata, handles)
edit1=str2double(get(hObject,'String'))*1000;
h1=dec2hex(edit1);
setappdata(0,'h1',h1);
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function edit2_Callback(hObject, eventdata, handles)
edit2=str2double(get(hObject,'String'))*1000;
h2=dec2hex(edit2);
setappdata(0,'h2',h2);
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function edit3_Callback(hObject, eventdata, handles)
edit3=str2double(get(hObject,'String'))*1000;
h3=dec2hex(edit3);
setappdata(0,'h3',h3);
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function edit4_Callback(hObject, eventdata, handles)
edit4=str2double(get(hObject,'String'))*1000;
h4=dec2hex(edit4);
setappdata(0,'h4',h4);
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function edit5_Callback(hObject, eventdata, handles)
edit5=str2double(get(hObject,'String'))*1000;
h5=dec2hex(edit5);
setappdata(0,'h5',h5);
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function edit6_Callback(hObject, eventdata, handles)
edit6=str2double(get(hObject,'String'))*1000;
h6=dec2hex(edit6);
setappdata(0,'h6',h6);
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function edit7_Callback(hObject, eventdata, handles)
edit7=str2double(get(hObject,'String'))*1000;
h7=dec2hex(edit7);
setappdata(0,'h7',h7);
function edit7_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
h1=getappdata(0,'h1');
h2=getappdata(0,'h2');
h3=getappdata(0,'h3');
h4=getappdata(0,'h4');
h5=getappdata(0,'h5');
h6=getappdata(0,'h6');
h7=getappdata(0,'h7');
X=h1+h2+h3+h4+h5+h6+h7;
% --------------------------------------------------------------------
function SaveButton_ClickedCallback(hObject, eventdata, handles)
% save the numbers the user put in the text boxes
% --------------------------------------------------------------------
function LoadButton_ClickedCallback(hObject, eventdata, handles)
% load the numbers and display them in the textboxes

  5 Comments

Use a mat file to save parameters that you read from your GUI and then load it in next time and assign them to your components.

Don't try to save handles and reload this, that is not a good idea at all.

Could you explain to me how I can save all my variables to one mat file?

Should I use getappdata?

Sorry I have no idea how to go on in this proeject.

You can save variables to a .mat file with the save command. You shouldn't save the handles, but the variables that stay the same. Then you you can use the load function to load the variable to a struct and copy them to the handles struct.

Sign in to comment.

Products


Release

R2018a

0 Answers