GUI help for collecting basic data

2 次查看(过去 30 天)
Hi, I am trying to collect basic data through edit text and have the data be stored in a matrix which the GUI outputs. This would allow me to use the data in some script. Following is what I have:
function varargout = personal_data(varargin)
% PERSONAL_DATA MATLAB code for personal_data.fig
% PERSONAL_DATA, by itself, creates a new PERSONAL_DATA or raises the existing
% singleton*.
%
% H = PERSONAL_DATA returns the handle to a new PERSONAL_DATA or the handle to
% the existing singleton*.
%
% PERSONAL_DATA('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in PERSONAL_DATA.M with the given input arguments.
%
% PERSONAL_DATA('Property','Value',...) creates a new PERSONAL_DATA or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before personal_data_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to personal_data_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 personal_data
% Last Modified by GUIDE v2.5 27-Feb-2018 21:32:06
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @personal_data_OpeningFcn, ...
'gui_OutputFcn', @personal_data_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 personal_data is made visible.
function personal_data_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 personal_data (see VARARGIN)
% Choose default command line output for personal_data
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes personal_data wait for user response (see UIRESUME)
uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = personal_data_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;
function name_Callback(hObject, eventdata, handles)
% hObject handle to name (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 name as text
% str2double(get(hObject,'String')) returns contents of name as a double
handles.name=str2double(get(hObject,'String'));
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function name_CreateFcn(hObject, eventdata, handles)
% hObject handle to name (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
function id_Callback(hObject, eventdata, handles)
% hObject handle to id (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 id as text
% str2double(get(hObject,'String')) returns contents of id as a double
handles.id=str2double(get(hObject,'String'));
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function id_CreateFcn(hObject, eventdata, handles)
% hObject handle to id (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
function age_Callback(hObject, eventdata, handles)
% hObject handle to age (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 age as text
% str2double(get(hObject,'String')) returns contents of age as a double
handles.age=str2double(get(hObject,'String'));
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function age_CreateFcn(hObject, eventdata, handles)
% hObject handle to age (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
function gender_Callback(hObject, eventdata, handles)
% hObject handle to gender (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 gender as text
% str2double(get(hObject,'String')) returns contents of gender as a double
handles.gender=str2double(get(hObject,'String'));
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function gender_CreateFcn(hObject, eventdata, handles)
% hObject handle to gender (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 button press in progress.
function progress_Callback(hObject, eventdata, handles)
% hObject handle to progress (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
pd= [handles.name ; handles.id ; handles.age ; handles.gender];
close;
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: delete(hObject) closes the figure
delete(hObject);
Currently, I get the following error:
Struct contents reference from a non-struct array object.
Error in personal_data>personal_data_OutputFcn (line 73)
varargout{1} = handles.output;
Error in gui_mainfcn (line 262)
[varargout{1:nargout}] = feval(gui_State.gui_OutputFcn, gui_hFigure, [], gui_Handles);
Error in personal_data (line 40)
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
Error in main (line 7)
Data= personal_data;
In addition to getting rid of the error, I would like the pd matrix (in progress_Callback function) to be output by the function when the push button is clicked. Do I make it a global variable? I cannot find a solution to have the data output by the GUI and I will be glad if you beautiful people could help!
  2 个评论
Jan
Jan 2018-2-28
编辑:Jan 2018-2-28
Your code is not readable. Don't you see this? Don't you want to care about code, which can be read by the ones, who want to help you? See How to format in the forum .
SARVESH AGRAWAL
SARVESH AGRAWAL 2018-3-14
Apologies. I should have known better.

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2018-2-28
What do you mean by "output the pd matrix". Output to where? Using global variables or creating a variable in the base workspace by evalin works, but it is prone to bugs, hard to maintain and clumsy. What do you want to do with the variable? Use it as input to another function? Then better start this other function directly from a callback of the GUI instead of inserting an indirection over creating an additional variable.
The variable handles is not the expected struct, so use the debugger to check, what it is instead:
dbstop if error
Run the code again until it stops. Then check the value of handles. Does this help you to recognize, where it could have been changed to something unexpected? We cannot do this for you, because without the FIG file we cannot run your code. Reading the code is hard also, as long as you do not format it.
  1 个评论
SARVESH AGRAWAL
SARVESH AGRAWAL 2018-3-14
I want the pd matrix to be the output of the GUI (line 1 where I currently have varargout). The matrix is just a collection of personal data such as name, age, etc. I want this to be the output of the GUI so that I can use it in the main script which runs this GUI after a welcome screen. Thank you.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by