フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

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

1 回表示 (過去 30 日間)
Mikkel
Mikkel 2013 年 4 月 19 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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