Improving resolution of the plots in Matlab
Show older comments
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
Walter Roberson
on 16 Oct 2022
exportgraphics() if your MATLAB is new enough. (You forgot to mark your version in the question.)
dpb
on 16 Oct 2022
See exportgraphics
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?
Go Sugar
on 18 Oct 2022
dpb
on 18 Oct 2022
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.
Go Sugar
on 22 Oct 2022
dpb
on 22 Oct 2022
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...
Go Sugar
on 22 Oct 2022
%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.
Answers (1)
Harsha
on 19 Oct 2022
1 vote
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: -
- https://in.mathworks.com/help/matlab/creating_plots/save-figure-at-specific-size-and-resolution.html
use exportgraphics to save only the right one and check if you are getting a better ouput.
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
