getframe(), rectangle specified in normalized coordinates instead of pixel coordinates
11 views (last 30 days)
Show older comments
I want to use the getframe() option to make a snapchat of a part of my figure. However, the documentation says that getframe(__, rect), the rectangle coordinates are in pixels. However, to ensure that I will get the same snapshot on another computer, how can I give rect in normalized coordinates?
rect = [65 385 720 255];
f = getframe(h.f, rect); %h.f is the parent container
f = frame2im(f);
0 Comments
Answers (1)
Simon Chan
on 25 Mar 2022
Try the following workaround
f = figure;
f.Units = 'normalize';
f.Position = [0.25 0.25 0.5 0.5]; % Location of the figure on the screen
ax = gca;
ax.Units = 'pixels';
plot(1:10,1:10);
ti = ax.TightInset;
pos = ax.Position;
rect = [-ti(1), -ti(2), pos(3)+ti(1)+ti(3), pos(4)+ti(2)+ti(4)];
F = getframe(ax,rect);
4 Comments
Eric
on 9 Mar 2023
May I ask why -ti(1) and -ti(2)?
Surely that will give negative pixel values that will cause getframe( ) to fall over?
In my example the 4th value (pos(4)+ti(2)+ti(4)) also gives a value that is outside the screen area (greater than its height).
In any case I get the dearly-loved error (with my code, not yours): "The specified rectangle is not fully contained within the figure. MATLAB no longer supports this capability."
Simon Chan
on 10 Mar 2023
Accoring to the Control Axes Layout mentioned here. The 'InnerPosition' is too small where the axes labels and title will not be captured while the 'OuterPosition' is too large which has too much empty space.
Extract the 'TightInset' property and added to the 'InnerPosition' to make the region larger:
pos = ax.Position; % InnerPosition of axis
rect = [-ti(1), -ti(2), pos(3)+ti(1)+ti(3), pos(4)+ti(2)+ti(4)];% Added the margin
Yes, they are negative values for the left and bottom points but they are relative to the axes only and still within the figure window. However, if you manually adjust bottom and left of the 'OuterPosition' property to some negative values, I think the above method is going to be fail.
Try again here and could you share your code.
f = figure;
f.Units = 'normalize';
f.Position = [0.05 0.25 0.4 0.7]; % Location of the figure on the screen
x = rand(1,10);
y = rand(1,10);
z = duration(rand(10,1),randi(60,10,1),randi(60,10,1));
plot3(x,y,z,'o','DurationTickFormat','mm:ss')
xlabel('X')
ylabel('Y')
zlabel('Duration')
grid on
ax = gca;
ax.Units = 'pixels'; % Set units to pixels
ti = ax.TightInset;
pos = ax.Position;
rect = [-ti(1), -ti(2), pos(3)+ti(1)+ti(3), pos(4)+ti(2)+ti(4)];
F = getframe(ax,rect);
f2 = figure;
imshow(F.cdata);
See Also
Categories
Find more on Animation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!