Figure appearance is different in debug mode

Hi,
I created a function to modify the appearance of figures (see attachement) and save it to different file formats. When I used the function the axis is not positioned as I intended, so I switched into debug mode. However, when I pause, the problem does not arrise. In contrary everything works out well. Thus, I don't know where the actual problem lies and how to fix it. Can anyone help me with that?
Best,
Stefan
function func_saveFig(fig,fileName,width,height,margin,fontSize,formatType)
% Save figure as predefined data type (e.g. pdf, png etc.) so it can be
% used in publication or presentations.
% input:
% fig Matlab figure handle that refers to figure that
% shall be saved
% fileName (optional) file name, i.e. target, of figure to be saved
% (string)
% width (optional) figure width in centimeter
% height (optional) figure height in centimeter
% margin (optional) margin width of figure in centimeter
% fontSize (optional) font size in pt (integer)
% formatType (optional) data format of saved figure (string, according to
% https://de.mathworks.com/help/matlab/ref/print.html)
% Stefan Wernitz
% Leibniz University Hannover
% Institute of Structural Analysis
% January 16, 2020
%% check input
% check minimum input
if nargin < 1
error('Too few input arguments')
end
% set default
if nargin < 2
fileName = 'fig1';
elseif isempty(fileName)
fileName = 'fig1';
end
if nargin < 3
width = 10;
elseif isempty(width)
width = 10;
end
if nargin < 4
height = 10;
elseif isempty(height)
height = 10;
end
if nargin < 5
margin = .5;
elseif isempty(margin)
margin = .5;
end
if nargin < 6
fontSize = 11;
elseif isempty(fontSize)
fontSize = 11;
end
if nargin < 7
formatType = '-dpdf';
elseif isempty(formatType)
formatType = '-dpdf';
end
%% save figure
set(fig, 'Windowstyle', 'normal')
% font
set(groot, 'DefaultTextInterpreter', 'latex');
set(groot, 'DefaultAxesTickLabelInterpreter', 'latex');
set(groot, 'DefaultAxesFontName', 'latex');
set(groot, 'DefaultLegendInterpreter', 'latex');
if ~isempty(get(ancestor(findobj(fig,'Type','Axes'), 'axes'), 'Colorbar'))
cBar = colorbar;
set(cBar, 'TickLabelInterpreter', 'latex');
end
% font size
set(findobj(fig,'Type','Axes'),'FontSize',fontSize);
% size
set(fig,'Units','centimeters','Position',[5 5 width height]);
% margins if only one subpplot
axAll = findobj(fig, 'Type', 'Axes' );
if length(axAll)==1
ax = findobj(fig, 'Type', 'Axes' );
set(ax,'Units','centimeter')
outerpos = ax.OuterPosition;
ti = ax.TightInset; % margin added by Matlab
ti = ti + margin;
left = outerpos(1) + ti(1);
bottom = outerpos(2) + ti(2);
ax_width = outerpos(3) - ti(1) - ti(3);
ax_height = outerpos(4) - ti(2) - ti(4);
ax.Position = [left bottom ax_width ax_height];
end
% paper and print
set(fig,'PaperUnits','centimeter','PaperPositionMode', 'auto','PaperSize', [width height]);
if strcmp(formatType,'-dpdf')
formatOption = '-r800'; % resolution 800 dpi
print(fileName,formatType,formatOption);
else
print(fileName,formatType);
end
end

5 Comments

I'm experiencing almost the same problem! If I run my code in debug mode, everything works fine. If I just run my code, then the figures' formats are off!! I have no idea why.
My code just produces graphs, without need for any interactive UI, so time delaying is not a constraint. I added a pause(0.001) right at the point where the normal code stopped doing what it was supposed to, and it now works.
I'm not happy with that solution, because it's just not elegant and could be a problem if I needed my program to respond fast. I'm also not sure why it worked! This might have given my graphics card the time it needed to update something?? Anyways, let me know if you find a better solution!
I am still encountering this error in R2022b as well!
I tried using drawnow and the pauses but that deosn't seem to work. I'm just making a 4x4 subplot and loading some data in. The subplots resize correctly when stepped through the debug mode but are completely skipped during a regular run call.
Hi guys, I am having the same problem with a function I've written for making plots. Also using R2022b.
I found a 1s pause didn't fix this issue, but my code did run properly when I added what I thought would be a ridiculously long pause of 5 seconds! I honestly only did that to rule out whether a pause was the right option but it worked. @john carr I don't know how much work your graphics renderer is having to do, but it might be worth double checking whether a longer pause could work for you.
john carr
john carr on 30 Jan 2023
Edited: john carr on 30 Jan 2023
I just tested this and @Rebecca Norris you're exactly correct. Adding a 5 second pause did cause the formatting to update. Weird... the plots are just simple lines with minimal formatting.
Ha, I am so glad it worked for you too! I agree it's weird and definitely a hack solution, I feel like there really shouldn't be that much lag.

Sign in to comment.

Answers (1)

Hi Matlab,
can you please fix this. It's ... 2023, and this bug (?) has, as far as I remember, been around for years. Surely adding a 5sec pause cannot be the solution.
Thank you.

Categories

Find more on Printing and Saving in Help Center and File Exchange

Asked:

on 21 Jan 2020

Answered:

on 4 Mar 2023

Community Treasure Hunt

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

Start Hunting!