GUI - Populating listbox with the contents of a folder.
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi, SOrry again, I am unable to comment on previous questions so need to start yet another question. So I have a browse pushbutton in gui and a listbox (listbox1) and I want to click 'browse', select a folder on my computer, then have the contents of that file (the names of each file/picture) be loaded into the listbox. So far I have this code in the pushbutton1 Callback:
% --- 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);
and this kind of works. All the picture files in the folder are retrieved but they are displayed in my command window (NOT the listbox) along with an error message. This is what appears:
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
Does anyone have any ideas on how to fix this? It is working but just not 'outputting' the files onto the listbox for some reason. A very helpful previous user "Orion" said it was probably something to do with the handles and I agree.
Many Thanks,
Ellis
댓글 수: 1
Adam
2016년 3월 2일
Why are you "unable to comment" on your previous questions? That sounds like something you need to find out and sort out if need be quite quickly as people will keep answering things that others have already answered otherwise!
답변 (2개)
Adam
2016년 3월 2일
The error suggests that 'handles' is not a struct any more which it should be.
This would be the case if, somewhere in your code you have done a guidata instruction in which you pass something other than the handles struct in e.g.
guidata( hObject, 6 )
would cause 'handles' in any subsequent callback to just be 6 instead of the handles struct.
With only what you have shown us it is impossible to say exactly what is causing the problem, but it does seem clear that 'handles' has been messed up prior to this function so look closely at the callbacks of any functions in the GUI that execute before this one.
Apart from that your code looks fine to do what you wish, though you probably want to terminate
MyListOfFiles = {Infolder(~[Infolder.isdir]).name}
with a ; to avoid having it print to command line!
댓글 수: 0
Jan
2016년 3월 2일
You can use the debugger to finde the cause of such problems. Either set a breakpoint in the failing line and start the code again. Or let Matlab stop automatically: Type this in the command window:
dbstop if error
and run the code again.
If Matlab stops, check the type of the variables used in the current line:
whos handles
disp(handles)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!