megrxu

Matlab 匯出合適大小的圖片

Jun 28, 2018  #Code 

昨天在寫通訊原理大作業時,想給TeX檔案插入PDF格式的圖片,於是嘗試用Matlab匯出生成圖片。結果發現匯出的PDF非常龐大,無論影象多小都匯出了A4大小的文件。

查閱了官方的文件,有幾行程式碼可以起作用。

% Exporting a figure in Matlab to PDF often outputs an A4-sized pdf file. This snippet can export a minimal size pdf which can be used in TeX files directly.
% some figures here
figure;
plot(zeros([1, 5]));
% codes that works
fig = gcf;
fig.PaperPositionMode = 'auto';
fig_pos = fig.PaperPosition;
fig.PaperSize = [fig_pos(3) fig_pos(4)];

實際使用時,如果使用的是GUI的匯出,需要先調整視窗大小到合適,然後再生成一次圖片。否則可能會報溢位視窗大小的錯誤。



可能相關的