How to wait the previous Appdesigner plot(app.UIAXes13, X1, Y1) is finished?

3 次查看(过去 30 天)
Hi! I am Jacob. Thanks for your help.
I am using Appdesigner.
I am drawing 3 graphs in one screen. All are working corretly. The codes are asfollows.
plot(app.UIAXes13, X1, Y1) % No problem of drawing
plot(app.UIAXes14, X2, Y2) % No problem of drawing
plot(app.UIAXes15, X3, Y3) % No problem of drawing
I add a screen capture function call after the above.
CaptureScreen(app); % Pleas refer the function codes below
Problem:
The captured image does not show plot of (app.UIAXed 13, 14, 15) correctly.
It seems the plots are pushed in a job queue. Even the graphs are not correctly drawn,
the capture is processed.
If I put a break point in front of "CaptureScreen(a00);", there is no problem.
So I am adding pause(2) before capturing, it is not correct approach.
I want to know how I can wait the previous plots a all correcly finished?
I appreciate for your help.
Jacob,
% Attachment --------------------------------------------------------------------------------------
function CaptureScreen(app)
robot = java.awt.Robot();
temp = app.HystTab.Position; % returns position as [left bottom width height]
pos = [temp(1)+40 temp(2)+200 temp(3)*1.49 temp(4)*1.47]; % [left top width height]
rect = java.awt.Rectangle(pos(1),pos(2),pos(3),pos(4));
cap = robot.createScreenCapture(rect);
% Convert to an RGB image
rgb = typecast(cap.getRGB(0,0,cap.getWidth,cap.getHeight,[],0,cap.getWidth),'uint8');
imgData = zeros(cap.getHeight,cap.getWidth,3,'uint8');
imgData(:,:,1) = reshape(rgb(3:4:end),cap.getWidth,[])';
imgData(:,:,2) = reshape(rgb(2:4:end),cap.getWidth,[])';
imgData(:,:,3) = reshape(rgb(1:4:end),cap.getWidth,[])';
imshow(imgData);
cd (app.WorkingExpFolder)
cd report
imwrite(imgData,app.ImgFileName)
close
end

回答(1 个)

Vedant Shah
Vedant Shah 2025-5-30
编辑:Vedant Shah 2025-5-30
In MATLAB App Designer, plot commands are typically queued for rendering. When a screen capture command is executed immediately after these plot commands, it may run before the rendering is complete, resulting in an incomplete or incorrect screenshot. This behaviour explains why introducing apauseor a breakpoint appears to resolve the issue as it allows sufficient time for the plots to finish rendering before the capture occurs. For more information regarding the same, refer to the following documentation:
A better approach is to explicitly force MATLAB to complete all pending graphics updates before proceeding. This can be achieved using thedrawnowfunction, which flushes the graphics event queue and ensures that all rendering tasks are completed before moving to the next line of code.
To address the issue, simply insert the following line before capturescreenfunction is called:
drawnow;
This ensures that all plots are fully rendered before the screen is captured. Below is the image that I obtained when trying to run the code after adding the above line of code:
For more information, please refer to the following MATLAB documentation:

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by