Error Using Array as Input to a GUIDE GUI

Hello, I'm having an issue trying to pass a string or cell array of strings into my GUIDE gui called HEPgui as a parameter. I'm trying to use this list to updated the contents of my listbox. I'm using MatLab 2017a.
Here is the error I'm receiving:
>> a = HEPgui('fp1');
Struct contents reference from a non-struct array object.
Error in HEPgui>listbox1_CreateFcn (line 113)
for k = 1 : length(handles.labels)
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in HEPgui (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)HEPgui('listbox1_CreateFcn',hObject,eventdata,guidata(hObject))
>>
Here is my code:
function varargout = HEPgui(varargin)
% HEPGUI MATLAB code for HEPgui.fig
% HEPGUI, by itself, creates a new HEPGUI or raises the existing
% singleton*.
%
% H = HEPGUI returns the handle to a new HEPGUI or the handle to
% the existing singleton*.
%
% HEPGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in HEPGUI.M with the given input arguments.
%
% HEPGUI('Property','Value',...) creates a new HEPGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before HEPgui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to HEPgui_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 HEPgui
% Last Modified by GUIDE v2.5 06-Jul-2018 18:03:34
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @HEPgui_OpeningFcn, ...
'gui_OutputFcn', @HEPgui_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 HEPgui is made visible.
function HEPgui_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 HEPgui (see VARARGIN)
% Choose default command line output for HEPgui
handles.output = hObject;
handles.labels = varargin{1};
handles.counter = 0;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes HEPgui wait for user response (see UIRESUME)
uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = HEPgui_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} = get(handles.listbox1, 'value');
%delete figure
delete(handles.figure1);
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox1
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox 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
for k = 1 : length(handles.labels)
listboxItems{k} = handles.labels(k).name;
end
set(handles.listbox1, 'String', listboxItems);
guidata(hObject,handle);
% --- 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
if isequal(get(hObject, 'waitstatus'), 'waiting')
uiresume(hObject); %the GUI is in uiwait
else
delete(hObject); %the GUI is no longer waiting
end

답변 (1개)

Walter Roberson
Walter Roberson 2018년 7월 7일

0 개 추천

You cannot use the handles structure in a CreateFcn callback that will be invoked before HEPgui_OpeningFcn . The handles structure is created by the call to gui_mainfcn which is not until all of the CreateFcn stored in the .fig have run.
Also, when the first argument you pass into HEPgui is datatype char, then the GUIDE created code will try to take a function handle to what it presumes is the name of a function, and then will try to invoke that function handle. To get around that, you will need to provide the name of a function in your HEPgui.m file as the first parameter, with your desired data as a second parameter, and the function would then do whatever processing is appropriate for the argument.

카테고리

도움말 센터File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

질문:

2018년 7월 6일

답변:

2018년 7월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by