Maintain image size on saving

Hello all,
I am having some frustrating problems with saving MATLAB figures and would be very grateful for help.
When I save a figure using the following code the dimensions of the image and its contents are not maintained upon saving:
saveas(gcf, name, 'tiffn'); saveas(gcf, name, 'eps'); saveas(gcf, name, 'jpg');
On the other hand
saveas(gcf,name, 'fig')
DOES maintain the dimensions.
However if I save the figure in these formats by clicking through the file menu of the image window then these dimensions ARE saved in the image files. I have tried changing the default figure size using
set(0,'defaultFigurePosition', [pos(1) pos(2) width, height]),
but although this works for creating the figures at the correct size it doesn't help with the problem above.
I would be grateful for any help with this. Having to click through to save each file individually is not fun!
Thanks
Marta

 採用された回答

Guillaume
Guillaume 2014 年 11 月 29 日

3 投票

I did report this issue (saveas vs file->save) over a year ago to mathworks and they pointed me to this answer. No idea, if this undocumented function still works in 2014b.
Bottom line, use hgsave

5 件のコメント

ML
ML 2014 年 11 月 30 日
The method in the linked answer
hgexport(gcf, 'figure1.jpg', hgexport('factorystyle'), 'Format', 'jpeg')
works thank you! hgsave() only saves to a .fig file though.
Image Analyst
Image Analyst 2014 年 11 月 30 日
編集済み: Image Analyst 2014 年 11 月 30 日
I believe export_fig() lets you specify an output size. You could specify the size of the figure in pixels and save it as a PNG image file.
Asiya Al-Busaidi
Asiya Al-Busaidi 2018 年 4 月 11 日
The hgexport() function is very useful. Thank you for sharing :)
Muhamad Luqman Mohd Nasir
Muhamad Luqman Mohd Nasir 2021 年 5 月 31 日
how can i save multiple pic at once after i do image segmentation with the same image size
Image Analyst
Image Analyst 2021 年 5 月 31 日
@Muhamad Luqman Mohd Nasir If you don't want to save them sequentially with imwrite() then I think you'd have to save them all "at once" using save():
save('mutiple pictures.mat', 'image1', 'image2', 'image3');

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2014 年 11 月 29 日

0 投票

The dimensions (size, rows and columns) of the image has nothing to do with it. The image is shrunk of magnified to fit onto the figure. So if your screen was 1600 by 1920 and your image was 3000 by 4000, and your figure was full screen, then it would shrink down the image to something less than 1600 by 1920 (because there's some gray blank padding around the axes/image on the figure). But doing saveas() should save the figure with the number of pixels that the figure is on your screen I would think. Again, forget about what size the image is - that doesn't count at all because it scales that to fit onto your figure.

4 件のコメント

ML
ML 2014 年 11 月 29 日
Thanks for your fast reply. My problem is that saveas() is behaving differently to when I select file->save on the figure window. file->save does exactly what you say and saves the figure with the same number of pixels as displayed on the screen, but when I use saveas() it saves to a different number of pixels and adjusts the linewidths and fonts automatically to that new figure size meaning that I cannot just resize the image for including in documents as lines and fonts are then the wrong size. I don't understand why these two methods of saving should work differently and would rather be using saveas().
ML
ML 2014 年 11 月 29 日
Sorry I did't pose the original question very accurately!
Image Analyst
Image Analyst 2014 年 11 月 29 日
OK, so you don't really mean an image like an image you've displayed on your figure. You're talking about the figure itself, like a screenshot.
ML
ML 2014 年 11 月 30 日
Yep exactly. Guillaume below has linked to a solution that I will try. Thanks for your help.

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

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

質問済み:

ML
2014 年 11 月 29 日

コメント済み:

2021 年 5 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by