Info

This question is closed. Reopen it to edit or answer.

GUI - displaying results to listbox(2)

1 view (last 30 days)
Ellis Berry
Ellis Berry on 17 Mar 2016
Closed: MATLAB Answer Bot on 20 Aug 2021
*I am unable to comment on answers on this computer I have no idea why, so I have to create a new question for the same issue I am sorry. Please note I am not doing this out of choice!*
Hi everyone, In the GUI I am making I have a pushbutton which runs a big section of code. What it does is take all the images in a certain file and performs some image processing on them whilst displaying certain results in my matlab command window. Now, I want these results to appear as the code runs in a listbox (listbox2) rather than my matlab command window, so over time, the listbox becomes more and more populated with my results? Here is what is given in my command window as the code runs. As you can see, as each picture is processed, it displays a new line of text confirming it has been done:
>> GUI_2 PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1383.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1384.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1385.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1386.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1387.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1388.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1389.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1390.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1391.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1392.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1393.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1394.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1395.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1396.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1397.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1398.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1399.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1400.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1401.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1402.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1403.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1404.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1405.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1406.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1407.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1408.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1409.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1410.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1411.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1412.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1413.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1414.JPG PASSED:H:\Documents\DIP\Normal Test Pics\IMG_1415.JPG Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1383.JPG The experiment is not complete yet. The number of Black Pixels is: 97
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1384.JPG The experiment is not complete yet. The number of Black Pixels is: 44
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1385.JPG The experiment is not complete yet. The number of Black Pixels is: 55
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1386.JPG The experiment is not complete yet. The number of Black Pixels is: 94
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1387.JPG The experiment is not complete yet. The number of Black Pixels is: 22
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1388.JPG The experiment is not complete yet. The number of Black Pixels is: 39
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1389.JPG The experiment is not complete yet. The number of Black Pixels is: 61
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1390.JPG The experiment is not complete yet. The number of Black Pixels is: 44
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1391.JPG The experiment is not complete yet. The number of Black Pixels is: 145
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1392.JPG The experiment is not complete yet. The number of Black Pixels is: 134
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1393.JPG The experiment is not complete yet. The number of Black Pixels is: 82
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1394.JPG The experiment is not complete yet. The number of Black Pixels is: 87
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1395.JPG The experiment is not complete yet. The number of Black Pixels is: 153
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1396.JPG The experiment is not complete yet. The number of Black Pixels is: 267
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1397.JPG The experiment is not complete yet. The number of Black Pixels is: 103
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1398.JPG The experiment is not complete yet. The number of Black Pixels is: 92
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1399.JPG The experiment is not complete yet. The number of Black Pixels is: 351
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1400.JPG The experiment is not complete yet. The number of Black Pixels is: 264
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1401.JPG The experiment is not complete yet. The number of Black Pixels is: 186
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1402.JPG The experiment is not complete yet. The number of Black Pixels is: 379
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1403.JPG The experiment is not complete yet. The number of Black Pixels is: 435
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1404.JPG The experiment is not complete yet. The number of Black Pixels is: 274
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1405.JPG The experiment is not complete yet. The number of Black Pixels is: 614
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1406.JPG The experiment is not complete yet. The number of Black Pixels is: 802
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1407.JPG The experiment is not complete yet. The number of Black Pixels is: 62
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1408.JPG The experiment is not complete yet. The number of Black Pixels is: 1497
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1409.JPG The experiment is not complete yet. The number of Black Pixels is: 442
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1410.JPG The experiment is not complete yet. The number of Black Pixels is: 510
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1411.JPG The experiment is not complete yet. The number of Black Pixels is: 1876
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1412.JPG The experiment is complete at this stage! The number of Black Pixels is: 3182
Time of Picture (Secs): 435
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1413.JPG The experiment is complete at this stage! The number of Black Pixels is: 2762
Time of Picture (Secs): 450
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1414.JPG The experiment is complete at this stage! The number of Black Pixels is: 3740
Time of Picture (Secs): 465
Now reading H:\Documents\DIP\Batch_processed_HSV\IMG_1415.JPG The experiment is complete at this stage! The number of Black Pixels is: 2758
Time of Picture (Secs): 480
and here is my 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. LOAD BUTTON
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);
% update handles with the inDir
handles.inDir = folder_name;
guidata(hObject,handles);
% --- Executes on button press in pushbutton2. SAVE BUTTON
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)
%Choose which folder processed pics will go in.
save_folder_name = uigetdir;
%Update handles with the outDir
handles.outDir = save_folder_name;
guidata(hObject,handles);
% --- Executes on button press in pushbutton3. %MAIN!!
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)
%The picture interval is entered in editbox2, if not, this error message
%appears.
Interval=str2num(char(get(handles.edit2,'String'))); %Prompt for user to enter camera interval.
if isempty(Interval)
errordlg('Error, please load pictures to be processed and enter a picture time interval.');
else
%In and out directories chosen by pushbutton1 and pushbutton2
outDir = handles.outDir;
inDir = handles.inDir;
includeSubdirectories = true;
% All extensions that can be read by IMREAD
imreadFormats = imformats;
supportedExtensions = [imreadFormats.ext];
% Add dicom extensions
supportedExtensions{end+1} = 'dcm';
supportedExtensions{end+1} = 'ima';
supportedExtensions = strcat('.',supportedExtensions);
% Allow the 'no extension' specification of DICOM
supportedExtensions{end+1} = '';
% Create a image data store that can read all these files
imds = datastore(inDir,...
'IncludeSubfolders', includeSubdirectories,...
'Type','image',...
'FileExtensions',supportedExtensions);
% Process each image using trial_3 (Image Processing toolbox app that let
% me set the HSV thresholds).
for imgInd = 1:numel(imds.Files)
inImageFile = imds.Files{imgInd};
% Output has the same sub-directory structure and file extension as
% input
outImageFile = strrep(inImageFile, inDir, outDir);
try
% Read
im = imds.readimage(imgInd);
% Process
im = trial_3(im);
% Write
if(isdicom(inImageFile))
dicommeta = dicominfo(inImageFile);
dicomwrite(im, outImageFile, dicommeta, 'CreateMode', 'copy');
else
imwrite(im, outImageFile);
end
disp(['PASSED:', inImageFile]);
catch allExceptions
disp(['FAILED:', inImageFile]);
disp(getReport(allExceptions,'basic'));
end
end
%Specify the folder where the files (Pictures) live. Chosen by pushbutton2
myFolder=handles.outDir;
%Get a list of all files in the folder with the desired file name pattern.
filePattern=fullfile(myFolder, '*.JPG');
theFiles=dir(filePattern);
for k=1:length(theFiles)
baseFileName=theFiles(k).name;
fullFileName=fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
image=imread(fullFileName);
white=nnz(image);
black=(numel(image)-white);
if black>(numel(image)*0.0193); %if black pixels is more than 1.94% of pixels in image, experiment complete. Value can be altered.
disp('The experiment is complete at this stage!')
fprintf('The number of Black Pixels is:');
disp(numel(image)-white);
disp('Time of Picture (Secs): ');
disp((k-1)*Interval); %Here, "Interval" is a variable chosen by user (15 secs, 30 secs etc)
else
disp('The experiment is not complete yet.')
fprintf('The number of Black Pixels is:');
disp(numel(image)-white);
end
set(handles.listbox2,'String', fullFileName);
guidata(hObject,handles);
end
end
% --- Executes on button press in pushbutton4. EXIT BUTTON
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 is there any idea how to do this? I have tried (as you can see) at the end of pushbutton3 to do 'set(handles.listbox2,'String', fullFileName); guidata(hObject,handles);' but all this does is show the last file in the listbox rather than all of them? So it just displays 'H:\Documents\DIP\Batch_processed_HSV\IMG_1415.JPG'.
Any ideas would be very very welcomed. Thankyou in advance. A helpful user called "Adam" suggested "Store all your results in a cell array within the loop and keep setting that as the 'String' of your listbox." but I do not know how to achieve this. If someone could be as kind as to tell me how to actually do this in the code I would be very grateful.
Cheers, Ellis

Answers (1)

Image Analyst
Image Analyst on 17 Mar 2016
Before the loop, do this:
caListBoxItems = cell(length(theFiles), 1);
Inside the for loop, replace this line
fprintf(1, 'Now reading %s\n', fullFileName);
with these
thisString = sprintf(1, 'Now reading %s', fullFileName);
fprintf('%s\n', thisString);
caListBoxItems{k} = thisString;
set(handles.listbox2, 'String', caListBoxItems);
drawnow; % Force immediate screen repaint.

This question is closed.

Community Treasure Hunt

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

Start Hunting!