Info

This question is closed. Reopen it to edit or answer.

How can I save my data in my editboxes to my .mat file?

1 view (last 30 days)
Mikkel
Mikkel on 19 Apr 2013
Closed: MATLAB Answer Bot on 20 Aug 2021
Hi
I need a code to save my numbers in my editboxes in gui to my .mat file
This is what I have:
function save_Callback(handles, ~, ~)
[filename, pathname] = uiputfile( ...
{'*.mat';'*.*'}, ...
'Save as');
save(filename, get(handles.edit1));
I've been looking at the other peoples questions to how they do it, and cant seem to see the solution that works for my,
I have named my editboxes: edit1, edit2, edit3, ..... and so on until edit42
what shall I write to get the saved into my .mat file, and do I need a drawnow;?
.
.
.
I've made a simplification:
function varargout = gui(varargin)
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)
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
function save_Callback(hObject, eventdata, handles)
[filename, pathname] = uiputfile( ...
{'*.mat';'*.*'}, ...
'Save as');
save(filename, get(handles.edit1,'String'));
function load_Callback(hObject, eventdata, handles)
[filename pathname] = uigetfile('*.mat','Select the MATLAB code file');
L = load( fullfile( pathname, filename ) );
But this doesn't work, I get the error:
Error using save
'23' is not a valid variable name.
I put 23 into the editbox and tried to save it.

Answers (1)

David Kusnirak
David Kusnirak on 19 Apr 2013
Edited: David Kusnirak on 19 Apr 2013
Hi,
if you want to get the written value in the edit box you should use
save(filename, get(handles.edit1,'value'));
the field is updated immediately after you write the value into your GUI
  3 Comments
David Kusnirak
David Kusnirak on 19 Apr 2013
Did you use GUIDE to create your gui or did you design your gui by programming? Because it seems, that you do not have the handle of the editbox in the 'handles' variable
Mikkel
Mikkel on 19 Apr 2013
I used GUIDE to create my gui. I dont know if this is the problem, because my editbox: edit1 looks like this:
function edit1_Callback(hObject, ~, ~)
% hObject handle to edit7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit7 as text
% str2double(get(hObject,'String')) returns contents of edit7 as a double
val = str2double(get(hObject,'string'));
if isnan(val)
errordlg('Not a number');
end
Do I need a handles in the callback to make it work?

Community Treasure Hunt

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

Start Hunting!