GUI - 'Struct contents reference from a non-struct array object' error.

2 次查看(过去 30 天)
Hi, I am part way through making a GUI for batch image processing. I have a listbox (listbox1) and a pushbutton (PUSHBUTTON1). The pushbutton1 is used for 'browsing' through my files to choose a folder and load the contents of that folder (a selection of images) to display on the listbox1. I click the 'browse' pushbutton and sure enough, it takes me to my files, but when I select the folder I want I get this error:
MyListOfFiles =
Columns 1 through 7
'IMG_1383.JPG' 'IMG_1384.JPG' 'IMG_1385.JPG' 'IMG_1386.JPG' 'IMG_1387.JPG' 'IMG_1388.JPG' 'IMG_1389.JPG'
Columns 8 through 14
'IMG_1390.JPG' 'IMG_1391.JPG' 'IMG_1392.JPG' 'IMG_1393.JPG' 'IMG_1394.JPG' 'IMG_1395.JPG' 'IMG_1396.JPG'
Columns 15 through 21
'IMG_1397.JPG' 'IMG_1398.JPG' 'IMG_1399.JPG' 'IMG_1400.JPG' 'IMG_1401.JPG' 'IMG_1402.JPG' 'IMG_1403.JPG'
Columns 22 through 28
'IMG_1404.JPG' 'IMG_1405.JPG' 'IMG_1406.JPG' 'IMG_1407.JPG' 'IMG_1408.JPG' 'IMG_1409.JPG' 'IMG_1410.JPG'
Columns 29 through 34
'IMG_1411.JPG' 'IMG_1412.JPG' 'IMG_1413.JPG' 'IMG_1414.JPG' 'IMG_1415.JPG' 'Whole_Trial_1_CAT…'
Struct contents reference from a non-struct array object.
Error in GUI_2>pushbutton1_Callback (line 112)
set(handles.listbox1,'String', MyListOfFiles);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in GUI_2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)GUI_2('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
So, as the files are displayed to my command window, it must be working, just not displaying them to my listbox and I can't figure out why! I suspect it is a problem with the 'handles' but I cant figure it out. Here is ALL my GUI code so far:
function varargout = GUI_2(varargin)
%GUI_2 M-file for GUI_2.fig
% GUI_2, by itself, creates a new GUI_2 or raises the existing
% singleton*.
%
% H = GUI_2 returns the handle to a new GUI_2 or the handle to
% the existing singleton*.
%
% GUI_2('Property','Value',...) creates a new GUI_2 using the
% given property value pairs. Unrecognized properties are passed via
% varargin to GUI_2_OpeningFcn. This calling syntax produces a
% warning when there is an existing singleton*.
%
% GUI_2('CALLBACK') and GUI_2('CALLBACK',hObject,...) call the
% local function named CALLBACK in GUI_2.M with the given input
% arguments.
%
% *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 GUI_2
% Last Modified by GUIDE v2.5 02-Mar-2016 16:10:54
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI_2_OpeningFcn, ...
'gui_OutputFcn', @GUI_2_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 GUI_2 is made visible.
function GUI_2_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 unrecognized PropertyName/PropertyValue pairs from the
% command line (see VARARGIN)
% Choose default command line output for GUI_2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI_2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GUI_2_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 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
% --- 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)
% --- Load up the listbox with tif files in folder handles.handles.ImageFolder
% get the folder
folder_name = uigetdir;
% get what is inside the folder
Infolder = dir(folder_name);
MyListOfFiles = {Infolder(~[Infolder.isdir]).name}
set(handles.listbox1,'String', MyListOfFiles);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[file,path]=uiputfile('*.jpg','Save Destination');
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
msgbox('Thankyou for using the Image Processing Tool');
pause(3);
close();
close();
% --- Executes on selection change in listbox2.
function listbox2_Callback(hObject, eventdata, handles)
% hObject handle to listbox2 (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 listbox2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox2
% --- Executes during object creation, after setting all properties.
function listbox2_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox2 (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
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (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 edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (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
So, if anyone can please help I would be very grateful.
Cheers, Ellis

回答(2 个)

Geoff Hayes
Geoff Hayes 2016-3-7
Ellis - put a breakpoint at the line of code that is generating the error
set(handles.listbox1,'String', MyListOfFiles);
Now repeat your steps. Launch the GUI (from the command line or by pushing the Run button in the editor) and press the pushbutton to select your folder. Now when the debugger pauses at the above line of code, look at the handles structure. Does it have a field named listbox1? The error message is indicating that this field doesn't exist. From your posted code, there are callbacks for a listbox1 but there are also callbacks for a listbox2. Did you perhaps delete the first one but not the callbacks for it? Does your GUI have two listboxes?
(I have tested your lines of code to populate the listbox and it works fine. )
  3 个评论
Ellis Berry
Ellis Berry 2016-3-7
Hi Geoff, thanks for your answer. I did as you said and I got it to work now fine with no errors! Thankyou very much.
William Chamberlain
William Chamberlain 2018-10-18
Hi Ellis; would be stoked if you could post your fixed code, so I can see what you had to change.

请先登录,再进行评论。


Luis Fernando Hoyos Cogollo

类别

Help CenterFile Exchange 中查找有关 Dialog Boxes 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by