How to capture whole app.UIFigure with exportapp?

I developed an app that adds fields based on user needs. When I use exportapp, it captures the max length of the screen, which I guess is the intended purpose for a screengrab. Is there a way to grab the entire UIFigure and save it as a pdf if the window is scrollable (i.e., some fields are cut off)? We need to see all of the fields in the pdf for it to be useful.
I also tried using exportgraphics() but when I try to export app.UIFigure as a pdf file, the pdf is blank.
Thank you.

Answers (1)

you could try to temporarly increase the figure size, export it and restore old behavior
%% create ui figure with three buttons, two of them out of sight
fig = uifigure(Position=[680 200 300 300],Scrollable="on");
uibutton(fig,Position=[100 150 100 22]);
uispinner(fig,Position=[400 150 100 22]); % too far to the right
uispinner(fig,Position=[100 10 100 22]);
fig.Position=[680 427 300 74]; % add vertical scrollability by setting small window size (just to show the concept of expanding along x and y)
% get components
allComps=fig.Children;
% get positions of components
positions=cell2mat(get(allComps,'Position'));
% get max extent of figure elements (x/y position plus x/y size)
maxX=max(sum(positions(:,[1 3]),2))*1.1;
maxY=max(sum(positions(:,[2 4]),2))*1.1;
% save old position
oldPos=fig.Position;
fig.Position(3:4)=[maxX maxY];
exportapp(fig,'expandedPDF.pdf');
% restore old position, size and scrollbars
fig.Position=oldPos; % old figure size, but tries to squeeze the buttons into the visible area
pause(1) % the figure squeezing needs some time, drawonw does not seem to do the job, pause helps here. If you leave out this line, the buttons will still be squeezed despite the next code line
% re set old positions of elements
arrayfun(@(in,row) set(in,'Position',positions(row,:)),allComps',1:numel(allComps))
pay attention to elements which are positioned at the edge of the figure, those are cut off when using exportapp. I could not fix this using code without
You can see the problem in the resulting pdf when e.g. setting
uispinner(fig,Position=[100 0 100 22]);
% instead of
uispinner(fig,Position=[100 10 100 22]);

3 Comments

Hi Jonas,
Appreciate the response. I tried replicating your code but I don't think it was working as intended. Even so, I think I understand what you are saying. If I change the position property, the position height is capped at the height of the screen, which I think is a related issue. Even if I maximize my app window, part of the app and fields are cut off and I need to scroll down to see them. When I use exportapp, it does capture the rest of the app after I scroll down but now I have the problem of having to have the user click the "Print/Save" button then scroll down click another "Print/Save" button. Then, the user would have to look at two different pdf files and determine whether or not any info/fields were cut out of the screengrab.
now i understand your issue. UIfigures cannot be bigger than screen size. How unfortunate
another more complicated option could be to take s scrollshot (I think thats the name in android)
you can scroll through your container using the scroll() command, each time taking an image/screenshot and stitching them together to a bigger image. Justl like in a panorama recording, but easier, since there is no distortion of the image
I didn't know about this panorama feature. I'll give it a shot!

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products

Release

R2022a

Asked:

on 8 Feb 2023

Commented:

on 9 Feb 2023

Community Treasure Hunt

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

Start Hunting!