Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

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

1 Ansicht (letzte 30 Tage)
Mikkel
Mikkel am 19 Apr. 2013
Geschlossen: MATLAB Answer Bot am 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.

Antworten (1)

David Kusnirak
David Kusnirak am 19 Apr. 2013
Bearbeitet: David Kusnirak am 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 Kommentare
David Kusnirak
David Kusnirak am 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 am 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?

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by