필터 지우기
필터 지우기

Why does text move when exporting figure using normalized units?

조회 수: 11 (최근 30일)
Dirk
Dirk 2023년 4월 5일
댓글: Jesse Chao 2023년 6월 23일
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')
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')
When saving the graphic from the figure File menu in the GUI, this does not happen; the text appears where it should.
Version: R2022b
Platform: macOS Monterey 12.6.3

답변 (2개)

Dinesh
Dinesh 2023년 4월 6일
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');
  댓글 수: 2
Dirk
Dirk 2023년 4월 6일
I'm afraid neither approach worked.
fh = figure;
ph = plot(1,1);
th = text(0.5, 0.5, 'Test');%,'units','normalized');
set(th,'units','normalized','position',[0.5 0.5]);
print test1.png -dpng
exportgraphics(fh,'test2.png')
saveas(fh,'test3.png')
Dirk
Dirk 2023년 4월 6일
Saving out the same figure from the GUI still produces expected results:

댓글을 달려면 로그인하십시오.


Jesse Chao
Jesse Chao 2023년 6월 22일
편집: Jesse Chao 2023년 6월 22일
Hello @Dirk,
I am experiencing the same issue. I am wondering if you were able to resolve it eventually?
Thank you very much.
Jesse
  댓글 수: 4
Dirk
Dirk 2023년 6월 22일
Thanks for the feedback and testing. Unfortunately, that approach does not work in my configuration.
exportgraphics(fh,'test4.png','ContentType','vector')
Warning: 'ContentType' parameter is ignored when it is set to 'vector' for image output.
However, when I output to a PDF file as you did (with or without appending), the placement of the text is correct. Not a resolution for me, unfortunately, but glad it is working for your purposes.
Dirk
Jesse Chao
Jesse Chao 2023년 6월 23일
Hello @Dirk,
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by