Why does text move when exporting figure using normalized units?
11 views (last 30 days)
Show older comments
When I export a figure to a graphics file (e.g., .png or .jpeg), text placed in an axis gets moved from how it appears in the Matlab figure and how it is defined to appear in the code when using normalized units.
Without normalized units, it exports the graphic as expected:
fh = figure;
ph = plot(1,1);
th = text(0.5, 0.5, 'Test');%,'units','normalized');
print test.png -dpng
exportgraphics(fh,'test1.jpg')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1346829/image.png)
However, when exporting the figure with text units normalized, I would expect the text to move to the center of the axis (position 0.5, 0.5), and so it appears on the figure in Matlab, but the saved graphic moves the text to the top right corner of the axis.
fh = figure;
ph = plot(1,1);
th = text(0.5, 0.5, 'Test','units','normalized');
print test.png -dpng
exportgraphics(fh,'test1.jpg')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1346834/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1346839/image.jpeg)
When saving the graphic from the figure File menu in the GUI, this does not happen; the text appears where it should.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1346844/image.png)
Version: R2022b
Platform: macOS Monterey 12.6.3
0 Comments
Answers (2)
Dinesh
on 6 Apr 2023
Hi Dirk.
Different exporting functions (For example "print", "exportgraphics", "saveas") in MATLAB might handle graphics objects differently when exporting figures. This could lead to differences in the appearance of the exported figure compared to the one displayed on the screen.
I don't have access to the specific platform to reproduce the issue. However, I suggest you try the following workaround:
The "saveas" function provides an alternative way to save your figure as an image file. You can try using this function to see if the text positioning is preserved correctly when using normalized units.
fh = figure;
ph = plot(1, 1);
th = text(0.5, 0.5, 'Test', 'units', 'normalized');
saveas(fh, 'test.png');
You can also try adjusting the text position after setting the normalized units by calling the "set" function on the "th" handle.
fh = figure;
ph = plot(1, 1);
th = text(0.5, 0.5, 'Test');
set(th, 'units', 'normalized', 'position', [0.5, 0.5]);
print('test.png', '-dpng');
exportgraphics(fh, 'test1.jpg');
Jesse Chao
on 22 Jun 2023
Edited: Jesse Chao
on 22 Jun 2023
I am experiencing the same issue. I am wondering if you were able to resolve it eventually?
Thank you very much.
Jesse
4 Comments
Jesse Chao
on 23 Jun 2023
I can truly understand your frustration.
I hope the @MathWorks Support Team can fix this issue and provide clearer instructions regarding the problem. In the meantime, I wish you the best of luck in finding a solution or a workaround.
Take care and good luck!
Jesse
See Also
Categories
Find more on Environment and Settings 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!