saving and loading data from and into gui from/to .mat file
3 次查看(过去 30 天)
显示 更早的评论
Hello sir, I'm working on GUIDE to save some data from the GUI to workspace. I have edit boxes and popup menus with some default values... its working fine when i alter the data in the edit boxes(Checking in the box) and for popup menus after selecting the value in the list.... now the probelm is, its not saving the default value as it is displaying in the edit boxes or popup menus... please help me out to sort this probelm
with the same example, how to get the data in the already saved .mat file and load that data in to the edit boxes or popup menu... i tried with several commands.. but nothing is working out.... my code looks like this
function varargout = example(varargin)
% EXAMPLE M-file for example.fig
% EXAMPLE, by itself, creates a new EXAMPLE or raises the existing
% singleton*.
%
% H = EXAMPLE returns the handle to a new EXAMPLE or the handle to
% the existing singleton*.
%
% EXAMPLE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in EXAMPLE.M with the given input arguments.
%
% EXAMPLE('Property','Value',...) creates a new EXAMPLE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before example_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to example_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help example
% Last Modified by GUIDE v2.5 11-Feb-2015 15:18:15
% Begin initialization code - DO NOT EDIT
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
% End initialization code - DO NOT EDIT
% --- Executes just before example is made visible.
function example_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to example (see VARARGIN)
% Choose default command line output for example
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes example wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = example_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in Save_pb.
function Save_pb_Callback(hObject, eventdata, handles)
Message=handles.text;
Status=handles.popup;
uisave({'Message','Status'},'New file');
uiwait(msgbox('data saved'));
% hObject handle to Save_pb (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function text_Callback(hObject, eventdata, handles)
% hObject handle to text (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.text=get(hObject,'String');
guidata(hObject, handles)
% Hints: get(hObject,'String') returns contents of text as text
% str2double(get(hObject,'String')) returns contents of text as a double
% --- Executes during object creation, after setting all properties.
function text_CreateFcn(hObject, eventdata, handles)
% hObject handle to text (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
% --- Executes on selection change in popup.
function popup_Callback(hObject, eventdata, handles)
% hObject handle to popup (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
contents=cellstr(get(hObject,'String'));
handles.popup=contents{get(hObject,'Value')};
guidata(hObject, handles)
% Hints: contents = cellstr(get(hObject,'String')) returns popup contents as cell array
% contents{get(hObject,'Value')} returns selected item from popup
% --- Executes during object creation, after setting all properties.
function popup_CreateFcn(hObject, eventdata, handles)
% hObject handle to popup (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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
% --- Executes on button press in load_pb.
function load_pb_Callback(hObject, eventdata, handles)
% hObject handle to load_pb (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename pathname] = uigetfile('*.mat','Select the MATLAB code file');
L = load( fullfile( pathname, filename ) );
varnames = fieldnames(L);
fieldvals =fieldvalue(varnames); %GUESS: you only have 1 variable in .mat
%GUESS: the one variable is a vector of numeric values
%GUESS: you have a vector of edit box handles
%GUESS: and each box is to get the textural representation of one value
set( handles.popup, 'String', fieldvals(1));
3 个评论
Adam
2015-2-16
You should be able to just save your hPopup.Value to file at any time. The default will be 1 if nothing has been user-selected.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!