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

3 ビュー (過去 30 日間)
Ellis Berry
Ellis Berry 2016 年 4 月 25 日
回答済み: Dave Behera 2016 年 5 月 2 日
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
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.');
%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,...
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)
inImageFile = imds.Files{imgInd};
% Output has the same sub-directory structure and file extension as
% input
outImageFile = strrep(inImageFile, inDir, outDir);
% Read
im = imds.readimage(imgInd);
% Process
im = trial_3(im);
% Write
dicommeta = dicominfo(inImageFile);
dicomwrite(im, outImageFile, dicommeta, 'CreateMode', 'copy');
imwrite(im, outImageFile);
disp(['PASSED:', inImageFile]);
catch allExceptions
disp(['FAILED:', inImageFile]);
waitbar(imgInd/perc, h);
%Specify the folder where the files (Pictures) live. Chosen by pushbutton2
%Get a list of all files in the folder with the desired file name pattern.
filePattern=fullfile(myFolder, '*.JPG');
caListBoxItems = cell(length(theFiles), 1);
for k=1:length(theFiles)
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.
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('Time of Picture (Secs): ');
disp((k-1)*Interval); %Here, "Interval" is a variable chosen by user (15 secs, 30 secs etc)
Status = ('Complete');
disp('The experiment is not complete yet.')
fprintf('The number of Black Pixels is:');
Status = ('Incomplete');
PhotoDetails={fullFileName, black, time, Status};
Matrix(k,:)=PhotoDetails; %Matrix of three variables produced.
Header={'Image', 'Number of Black Pixels', 'Time (Seconds)', 'Status'};
xlswrite('PhotoResults', Header, 'Results');
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.

回答 (1 件)

Dave Behera
Dave Behera 2016 年 5 月 2 日
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
X1 = rand(1,200);
Y1 = rand(1,200);
sc.XData = X1;
sc.YData = Y1;


Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by