GUI - Plotting graph iteratively

2 views (last 30 days)
Ellis Berry
Ellis Berry on 23 Mar 2016
Answered: Adam on 23 Mar 2016
Hi everyone, So I have made a GUI that successfully processes images in batch. Part of the processing is that it counts the number of black pixels in the image and also a 'time' for the image. So for example the first image is 0 seconds, second image is 30 seconds, 3rd image is 60 seconds etc.. Now, I have a loop which, as it runs, displays the variable 'black' and 'time' to the command window. How can I get my GUI, during this loop, to plot 'time' vs 'black' and keep adding iteratively to it as the loop goes round? Here is my code so far that happens when I push the pushbutton. The idea is to get the graph drawing in 'real time' as the code runs.
% --- 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 before clicking Run.');
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);
h=waitbar(0, 'Please Wait...');
% Process each image using trial_3 (Image Processing toolbox app that let
% me set the HSV thresholds).
for imgInd = 1:numel(imds.Files)
perc=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
waitbar(imgInd/perc, h);
drawnow;
end
delete(h);
%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);
caListBoxItems = cell(length(theFiles), 1);
for k=1:length(theFiles)
baseFileName=theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
thisString = fprintf(1, 'Now reading %s', fullFileName);
fprintf('%s\n', thisString);
caListBoxItems{k} = thisString;
OutputFileNames{k} = theFiles(k).name;
set(handles.listbox2, 'String', OutputFileNames); %listbox2 will display file names of processed images.
drawnow; % Force immediate screen repaint.
image=imread(fullFileName);
white=nnz(image);
black=(numel(image)-white);
time=((k-1)*Interval);
if black>(numel(image)*0.0193); %if black pixels is more than 1.93% 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
PhotoDetails={fullFileName, black, time};
Matrix(k,:)=PhotoDetails;
guidata(hObject,handles);
end
end
Header={'Image', 'Number of Black Pixels', 'Time (Seconds)'};
xlswrite('PhotoResults', Header, 'Results');
xlRange='A2';
xlswrite('PhotoResults', Matrix, 'Results', xlRange);
Any ideas? Cheers, Ellis

Answers (1)

Adam
Adam on 23 Mar 2016
I can't see in amongst your code exactly where this needs to fit as I have limited time, but here is a quick example of doing something similar:
figure; hAxes = gca;
hPlot = plot( 1, 1 );
for n = 2:100
pause( 1 )
hPlot.XData = [ hPlot.XData n ];
hPlot.YData = [ hPlot.YData n.^2 ];
end
You should be able to use the same idea for your work, replacing that hard-coded 1 in the pause instruction with whatever is appropriate (obviously it can be a variable) and adding your 'time' and 'black' values to the X and Y data as appropriate.
You will have to decide what you want to do with respect to the axes - in my above example they keep growing. If you know what their ultimate extent will be you could set the axes XLim and YLim to these upfront so that they remain static and the graph just grows within that fixed sized axes domain. Or you can leave it as above and allow the domain to keep expanding to encompass the new data.

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!