フィルターのクリア

How to save a figure of a specific size with exportgraphics

46 ビュー (過去 30 日間)
Blue
Blue 2021 年 10 月 13 日
コメント済み: Blue 2021 年 10 月 14 日
Hello,
I simply want to export a figure of a specific size (6 x 9 inches) with the function exportgraphics as described here (https://www.mathworks.com/help/matlab/creating_plots/save-figure-at-specific-size-and-resolution.html)
The following code doesnt return any errors but the figure is empty. Any tips ?
Thank you,
t = tiledlayout(1,1,'Padding','tight');
t.Units = 'inches';
t.OuterPosition = [0.25 0.25 6 9];
nexttile;
figure
subplot(2,1,1);
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
subplot(2,1,2);
y2 = sin(5*x);
plot(x,y2)
exportgraphics(t, 'test.jpg', 'Resolution', 300)
  1 件のコメント
Mario Malic
Mario Malic 2021 年 10 月 13 日
Hi,
You need to specify the parent figure to use the exportgraphics.
I am unable to figure out completely what's happening in the code.
This might do it.
fig = gcf;
exportgraphics(fig, 'test.jpg', 'Resolution', 300)

サインインしてコメントする。

採用された回答

Dave B
Dave B 2021 年 10 月 13 日
編集済み: Dave B 2021 年 10 月 13 日
you created a tiledlayout in one figure, set some of its characteristics but didn't add anything to it. Then you created a new figure with subplots, then you exported the (empty) tiledlayout.
Instead, use tiledlayout to set your layout shape, drop the call to figure, and use nexttile in place of the calls to subplot:
t = tiledlayout(2, 1, 'Padding', 'tight');
t.Units = 'inches';
t.OuterPosition = [0.25 0.25 3 3];
nexttile;
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
nexttile;
y2 = sin(5*x);
plot(x,y2)
exportgraphics(t, 'test.jpg', 'Resolution', 300)
  3 件のコメント
Dave B
Dave B 2021 年 10 月 14 日
How about:
t = tiledlayout(2, 1, 'Padding', 'tight');
t.Units = 'inches';
t.OuterPosition = [0.25 0.25 6 6];
nexttile;
x = linspace(0,10);
y1 = sin(x);
plot(x,y1)
nexttile;
y2 = sin(5*x);
plot(x,y2)
% if you want your figure to go back to where it was after export, you can
% store the current units and position and set them back after exporting
set(gcf,'Units','inches','Position',[1 1 8 8]) % I used width and height of 8 to be sure nothing got cut off
exportgraphics(t, 'test.jpg', 'Resolution', 300)
Blue
Blue 2021 年 10 月 14 日
thank you

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePrinting and Saving についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by