Specify "plot box" position for tiled layout
이전 댓글 표시
I am creating a png file from a plot and I need to specify the exact size of the figure and the plot area in pixels. I am using a tiled layout and set(gcf, 'Position', ...) doesn't seem to work.
Here a test.
The function figSave2 is a modified version of a function I found here in the Community. I added the 'InnerPosition' argument.
X = 1:100;
Y = 1:100;
figure;
tiledlayout(3,1,'TileSpacing','None');
for i=1:3
nexttile(i);
plot(X,Y,'b-');
end
figSave2(gcf,'testFileSave2.png',[2400 900],[166 0 2100 900]);
close( gcf );
function figSave2(fig,file,rez,plotbox,txt,bak)
%Save figure as image, with custom resolutions and text scaling.
% figsave(fig,file,rez,plotbox,txt,bak)
%
%Example:
% clf,text(0.1,0.5,{'This text should be';'50 pixels high';'and the image';'900W x 600H pix'},'FontSize',50)
% figSave(gcf,'Figure.jpg',[900 600],[100 100 700 400])
%
%
% https://www.mathworks.com/matlabcentral/answers/272767-how-to-set-exact-figure-size-in-pixels
if nargin<1 || isempty(fig), fig = gcf; end %figure handle
if nargin<2 || isempty(file), file = 'Figure.jpg'; end %output file name
if nargin<3 || isempty(rez), rez = [900 600]; end %resolution [horizontal vertical]
if nargin<4 || isempty(plotbox), plotbox = [100 100 700 400]; end %resolution [horizontal vertical]
if nargin<5 || isempty(txt), txt = 1; end %text scale factor
if nargin<6 || isempty(bak), bak = 0; end %dont preserve background colour
set(fig,'PaperPosition',[0 0 rez/(100*txt)],'InnerPosition',plotbox/(100*txt),'PaperUnits','inches'); %set paper size (does not affect display)
if bak
set(fig,'InvertHardcopy','off'); %preserve background colour
end
imwrite(print(gcf,'-RGBImage',['-r' num2str(100*txt,'%f')]),file) %print RGB image to file (slow)
end
The resulting plot does not have the specified offset of 166 pixels to the Y axes, it's more like 140.

How can I set the postion of the "plot box" that is made up from the three tiled plots?
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
