MATLAB Answers

Plotting graph on GUI (I can't comment on answers!! :( )

1 view (last 30 days)
Ellis Berry
Ellis Berry on 25 Apr 2016
Answered: Dave Behera on 2 May 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' (Number of black pixels) 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.
Here is my code so far for when I press 'pushbutton3' which runs the code:
% --- 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)
Status = ('Complete');
else
disp('The experiment is not complete yet.')
fprintf('The number of Black Pixels is:');
disp(numel(image)-white);
Status = ('Incomplete');
end
PhotoDetails={fullFileName, black, time, Status};
Matrix(k,:)=PhotoDetails; %Matrix of three variables produced.
guidata(hObject,handles);
end
end
Header={'Image', 'Number of Black Pixels', 'Time (Seconds)', 'Status'};
xlswrite('PhotoResults', Header, 'Results');
xlRange='A2';
xlswrite('PhotoResults', Matrix, 'Results', xlRange);
Any ideas? I have put axes on my GUI tagged "axes1" but I cant get it to plot! I am thinking I need to create like a database or matrix of data for the variables 'black' and 'time' then plot them? How do I do this? Any help would be greatly appreciated. Many thanks, Ellis
In answer to Geoff Hayes from my last question: Hi Geoff, the problem I'm having is how to even start plotting at all? As my code runs, the variables 'black' and 'time' are computed and I need a way of plotting these against each other? I can't simply use for k=1:1000 for example? I CANT REPLY TO ANSWERS ON THIS COMPUTER ANNOYINGLY SORRY, that is why I have asked this question on more than one thread :(
Thanks Guys.

Answers (1)

Dave Behera
Dave Behera on 2 May 2016
You will need to update the axes of your graph periodically and use a pause to let it refresh.
Try running the following snippet and see how it works:
x = rand(1,100);
y = rand(1,100);
sc = scatter(x,y);
for i = 1:5
pause(2)
X1 = rand(1,200);
Y1 = rand(1,200);
sc.XData = X1;
sc.YData = Y1;
end

Community Treasure Hunt

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

Start Hunting!