Improving resolution of the plots in Matlab

Hello :)
I have several matlab plots. When I try to save them in jpg or emf formats, I get poor resolution. I want to obtain a similar resolution like when I save it from matlab.fig file to jpg or emf (with a full screen). Since I have many figures, that is wasting time. Below is the simplest command for a few figures. Is there a way to improve the resolution with saveas command? And do I have to copy and paste each figure to transfer it into word document? I have searched the forum and could not find any suggestions with saveas command for improving the resolution.
Thank you so much. =)
for b=1:4
saveas(figure(b),['FIG' num2str(b'),'.jpg']);
saveas(figure(b),['FIG' num2str(b'),'.emf']);
saveas(figure(b),['FIG' num2str(b'),'.fig']);
end

10 Comments

exportgraphics() if your MATLAB is new enough. (You forgot to mark your version in the question.)
Go Sugar
Go Sugar on 16 Oct 2022
Edited: Go Sugar on 16 Oct 2022
I have both 2022 and 2014 versions. I tried exportgraphics instead of saveas and I tried saving as jpg and emf, I still cannot obtain a beautiful figure like saving from full screen mode of matlab.fig file to jpg or emf. Do you have any suggestions? I replaced exportgraphics instead of saveas like below:
exportgraphics(figure(b),['FIGURE' num2str(b'),'.emf'],'Resolution',300)
Thank you. =)
Resolution — Resolution (DPI)
150 (default) | whole number
Resolution in dots per inch (DPI), specified as a whole number that is greater than or equal to 1.
Specifying the resolution has no effect when the ContentType is 'vector'.
So when you wrote
exportgraphics(figure(b),['FIGURE' num2str(b'),'.emf'],'Resolution',300)
".emf" is a vector format so the resoltuion optional parameter isn't of any use...but a vector format should be rendered cleanly. How are you observing the result?
Thank you for the comments about resolution and emf. I also tried for jpg. But the plot I want is not as neat as (right one) when I save it from matlab.fig to jpg (left one). I am sending a part of the two outputs. Right one is more adjacent, mixed, lower resolution and mixed, whereas left one is better in all ways. Is there a way I can set the properties so that I can obtain a better plot that I have just mentioned? Thank you for your time. =)
That's not the resolution of the plot, that's cramming too much info into a given space with fonts that are too big and then comparing that to something else entirely different.
Clean up the plot first....you may have to find another way to present the data if you have so many bars in one axis.
We can't do anything about that without the data, but the problem isn't in the resolution, per se.
Thank you so much for the information, we now know at least the problem is not the resolution. =)
It really is interesting to have different figures while saving directly from matlab.fig or saveas/export graphics command.
I will try finding another way to represent the data like your suggestion. If I find it, I will also post it here.
Thank you for your time. =)
You didn't show data/code that generated the above but one thing you need to do when writing the values on the bars besides reducing the fontsize so not taller than the width of the bars is to use
hT=text(hB.XEndPoints,hB.YEndPoints,[" "+string(hB.YEndPoints)],'Rotation',90);
to add a little white space before the text so it isn't on top of the bar...and then as noted before, set the 'fontsize' so it isn't too big. I so rarely have the need to print figures, I'm not positive whether there's an issue on doing so that the respective rendered font and bar sizes are munged relative to the displayed or not...
Oh sorry I forgot to add the code:
Here is the text part:
text(1:length(mydata),mydata,num2str(mydata'),'rotation', 90, 'horiz','center');
Thank you so much. I appreciate your support. =) I will set the font size too.
But font sizes are more normal with my sample left attached figure since I see a better figure when I save it from matlab.fig output. =) Maybe this has to do with Matlab's saveas and exportgraphics commands that saves a bit different than from matlab.fig output.
%text(1:length(mydata),mydata,num2str(mydata'),'rotation', 90, 'horiz','center');
That's at least one problem -- using 'horizonatlalignment','center' is what is causing the values to be written on top of the bar that contributes greatly to the "messy" look.
Remember that the horizontal and vertical alignments are of the text itself with respect to its own orientation--when you rotate it 90 degrees, 'horizonatalalignment' is STILL with respect to the alignment of the text string from its origin; it is NOT related to the vertical/horizontal direction in the axes -- even though they're the same without rotation, they're flipped when the text is rotated.
Y=randi(100,1,20);
subplot(2,1,1)
hB=bar(Y); ylim([0 100])
text(hB.XEndPoints,hB.YEndPoints,string(hB.YEndPoints),'rotation',90,'horizontalalign','center');
title('Rotated Text, Centered Horizontal Alignment')
subplot(2,1,2)
hB=bar(Y); ylim([0 100])
hT=text(hB.XEndPoints,hB.YEndPoints,[" "+string(hB.YEndPoints)],'Rotation',90);
title('Rotated Text, Left (Default) Horizontal Alignment')
%subplot(3,1,3)
%hB=bar(Y);
%text(hB.XData,hB.YData," "+hB.YData,'rotation',90)
%title('Rotated Text, Left Horizontal Alignment Plus Extra Space')
It would be better/more robust coding to use the X,Y data and positions from the bar object itself (and more generic as well, you don't have to change code if the data change) instead of the hardcoded 1:N above -- what if you actually have a real X variable with other than ordinal values or a categorical??? The above is then wrong where as the other would still work.
Go Sugar
Go Sugar on 22 Oct 2022
Edited: Go Sugar on 22 Oct 2022
Thank you so much for all the help, you are so nice. =) Now I will be able to fix the messy look of the numbers. =) It will work fine for me. I cannot even put a vote to your comment but it solved the messy look of the numbers. =) In this way, everything looks more organized. Have a great day!

Sign in to comment.

Answers (1)

The “saveas” function uses a resolution of 150 DPI and uses “PaperPosition” and “PaperPositionMode” properties of the figure to determine the size of the image. There is no way to change the resolution of the output using the “saveas” command.
To control the size or resolution when you save a figure, use “print” function instead.
Refer to the following documentation for using the “print” function: -
From MATLAB R2020a you can use the “exportgraphics” function to save any axes, figure, chart that can be child of a figure, tiled chart layout, or container such as panel with specific size, resolution, or background color.
Refer to the following documentation on using “exportgraphics” function: -
use exportgraphics to save only the right one and check if you are getting a better ouput.

1 Comment

Thank you so much for the information. I will check the links provided, be sure that I am not missing anything and see if I get a better output. I will also try representing the data in another way, I have so many bars. I will let you know if I can solve it thank you for your time. =)

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 16 Oct 2022

Edited:

on 22 Oct 2022

Community Treasure Hunt

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

Start Hunting!