Info

此问题已关闭。 请重新打开它进行编辑或回答。

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

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

回答(1 个)

David Kusnirak
David Kusnirak 2013-4-19
编辑:David Kusnirak 2013-4-19
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 个评论
David Kusnirak
David Kusnirak 2013-4-19
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 2013-4-19
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!

Translated by