MATLAB Answers

Capturing and Saving an image of a running using code in Matlab's Appdesigner

118 views (last 30 days)
Hello,
I'm working on an app in Matlab Appdesigner. I have an idea to save an image of the app, like a screenshot equivalent, but using code. What I have in mind is to allow the user to press a button that performs a "screen capture" of the current app state.
I am using Matlab version 9.3.0.713579 (R2017b). Thank you in advance for your suggestions and help.

  0 Comments

Sign in to comment.

Accepted Answer

Ashutosh Prasad
Ashutosh Prasad on 2 Aug 2018
Hello,
In MATLAB Appdesigner, the created apps are displayed in UI figure which is different from the conventional figure in the way that many of the MATLAB functionalities is unsupported with the figures created using the uifigure command like saveas, savefig etc. Please refer to this page to know about the functions which are not supported with uifigure.
Although you can explicitly take the screenshot of your app and save it in your current directory either by using the ScreenCapture – Submission to File Exchange or by using the Java Libraries . Following is the code of a callback function to a pushbutton which captures the App.
% Button pushed function: CaptureButton
function CaptureButtonPushed(app, event)
robot = java.awt.Robot();
temp = app.UIFigure.Position; % returns position as [left bottom width height]
pos = [temp(1) temp(2)+temp(4) temp(3) temp(4)]; % [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);
end

  2 Comments

Harsh Trivedi
Harsh Trivedi on 17 Jul 2020
@ Ashutosh Prasad,
How would I call this function in AppDesigner code? Will it be inside button pushed function?

Sign in to comment.

More Answers (2)

Dan Young
Dan Young on 5 Dec 2019
This code worked nearly perfectly, but I found the second input to the java rectangle actually had to be the current monitor height MINUS the upper-left pixel value (which is bottom+height in typical Matlab positions).
So my working code first identifies the monitor the image is on, then gets it's height, and then uses that in the call to the javascript rectangle.
% Button pushed function: CaptureButton
robot = java.awt.Robot();
temp = app.UIFigure.Position; % returns position as [left bottom width height]
allMonPos = get(0,'MonitorPositions');
curMon = find(temp(1)<(allMonPos(:,1)+allMonPos(:,3)),1,'first');
curMonHeight = allMonPos(curMon,4)+1;
pos = [temp(1) curMonHeight-(temp(2)+temp(4)) temp(3)-1 temp(4)]; % [left top width height].... UL X, UL Y, 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,[])';
imclipboard('copy', imgData)

  0 Comments

Sign in to comment.


Adam Danz
Adam Danz on 20 Oct 2020
Staring in Matlab r2020b, the exportapp() function saves an image of your app or uifigure.
See this Community Highlight for a demo.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!