Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

running export_fig via windows 7 task scheduler when user is not logged in #152

Open
gborrageiro opened this issue May 26, 2016 · 1 comment

Comments

@gborrageiro
Copy link

gborrageiro commented May 26, 2016

hi there,

export_fig seems to work fine when running matlab when you are logged into the machine.
When running matlab via task scheduler and with the job set to run irrespective of whether the user is logged in or not, the plots become compressed and illegible.

When setting figure handle properties such as 'PaperPosition', it would be great if export_fig could interpret these, if it doesn't already.

I can't seem to attach files, so here is the function script:

function plotTest()
% plotTest();

%% initial setup
t = (1:30)';

%% create plot
close all
hfig = figure('Name','plot test','NumberTitle','off','units','normalized','outerposition',[0 0 1 1]);

for i=1:6
  ax(i) = subplot(3,3,i);
  X = cumsum(randn(30,5));

  h = plot(t,X(:,1),':o',t,X(:,2),':d',t,X(:,3),':s',t,X(:,4),':^',t,X(:,5),':x');

  set(h(1), 'MarkerFaceColor', get(h(1), 'Color'));
  set(h(2), 'MarkerFaceColor', get(h(2), 'Color'));
  set(h(3), 'MarkerFaceColor', get(h(3), 'Color'));
  set(h(4), 'MarkerFaceColor', get(h(4), 'Color'));
  set(h(5), 'MarkerFaceColor', get(h(5), 'Color'));

  grid on
  legend('x1','x2','x3','x4','x5')
end

hfig = tightfig(hfig);
pause(1)

%% collect debugging information
diary('C:\temp\myLogFile.txt')
fprintf('\n gcf: \n')
get(gcf)
res = get(groot,'ScreenPixelsPerInch');
fprintf(['\nresolution = ' num2str(res) '\n'])
currentScreenSizeFromOS = get(groot, 'ScreenSize');
fprintf(['current screen size from OS = ' num2str(currentScreenSizeFromOS) '\n\n'])
diary off

%% maximizePlot
hFig = gcf;
jFig = get(handle(hFig), 'JavaFrame');
jFig.setMaximized(true);

%% normal printing and exporting
print(hfig, 'C:\temp\plotTest_print.png', '-dpng', '-r300');

%% printing by specifying paper position
preferredScreenSize = [0 0 2560 1440]; % in pixels
hfig.PaperUnits = 'inches';
hfig.PaperPosition = preferredScreenSize./res; % convert to inches
print(hfig, '-dpng', '-r96', 'C:\temp\plotTest_printPaperPos.png')
supertitle('page 1 buddy','FontSize',16)
export_fig('-pdf', '-q100','handle',hfig,'C:\temp\plotTest_printPaperPos.pdf')
supertitle('page 2 buddy','FontSize',16)
export_fig('-pdf', '-q100','-append','handle',hfig,'C:\temp\plotTest_printPaperPos.pdf')

close

end
@gborrageiro
Copy link
Author

gborrageiro commented May 26, 2016

here is tightfig:

function hfig = tightfig(hfig)
% tightfig: Alters a figure so that it has the minimum size necessary to
% enclose all axes in the figure without excess space around them.
%
% Note that tightfig will expand the figure to completely encompass all
% axes if necessary. If any 3D axes are present which have been zoomed,
% tightfig will produce an error, as these cannot easily be dealt with.
%
% hfig - handle to figure, if not supplied, the current figure will be used
% instead.

if nargin == 0
  hfig = gcf;
end

% There can be an issue with tightfig when the user has been modifying
% the contnts manually, the code below is an attempt to resolve this,
% but it has not yet been satisfactorily fixed
%     origwindowstyle = get(hfig, 'WindowStyle');
set(hfig, 'WindowStyle', 'normal');

% 1 point is 0.3528 mm for future use

% get all the axes handles note this will also fetch legends and
% colorbars as well
hax = findall(hfig, 'type', 'axes');

% get the original axes units, so we can change and reset these again
% later
origaxunits = get(hax, 'Units');

% change the axes units to cm
set(hax, 'Units', 'centimeters');

% get various position parameters of the axes
if numel(hax) > 1
  %         fsize = cell2mat(get(hax, 'FontSize'));
  ti = cell2mat(get(hax,'TightInset'));
  pos = cell2mat(get(hax, 'Position'));
else
  %         fsize = get(hax, 'FontSize');
  ti = get(hax,'TightInset');
  pos = get(hax, 'Position');
end

% ensure very tiny border so outer box always appears
ti(ti < 0.1) = 0.15;

% we will check if any 3d axes are zoomed, to do this we will check if
% they are not being viewed in any of the 2d directions
views2d = [0,90; 0,0; 90,0];

for i = 1:numel(hax)

  set(hax(i), 'LooseInset', ti(i,:));
  %         set(hax(i), 'LooseInset', [0,0,0,0]);

  % get the current viewing angle of the axes
  [az,el] = view(hax(i));

  % determine if the axes are zoomed
  iszoomed = strcmp(get(hax(i), 'CameraViewAngleMode'), 'manual');

  % test if we are viewing in 2d mode or a 3d view
  is2d = all(bsxfun(@eq, [az,el], views2d), 2);

  if iszoomed && ~any(is2d)
    error('TIGHTFIG:haszoomed3d', 'Cannot make figures containing zoomed 3D axes tight.')
  end

end

% we will move all the axes down and to the left by the amount
% necessary to just show the bottom and leftmost axes and labels etc.
moveleft = min(pos(:,1) - ti(:,1));

movedown = min(pos(:,2) - ti(:,2));

% we will also alter the height and width of the figure to just
% encompass the topmost and rightmost axes and lables
figwidth = max(pos(:,1) + pos(:,3) + ti(:,3) - moveleft);

figheight = max(pos(:,2) + pos(:,4) + ti(:,4) - movedown);

% move all the axes
for i = 1:numel(hax)

  set(hax(i), 'Position', [pos(i,1:2) - [moveleft,movedown], pos(i,3:4)]);

end

origfigunits = get(hfig, 'Units');

set(hfig, 'Units', 'centimeters');

% change the size of the figure
figpos = get(hfig, 'Position');

set(hfig, 'Position', [figpos(1), figpos(2), figwidth, figheight]);

% change the size of the paper
set(hfig, 'PaperUnits','centimeters');
set(hfig, 'PaperSize', [figwidth, figheight]);
set(hfig, 'PaperPositionMode', 'manual');
set(hfig, 'PaperPosition',[0 0 figwidth figheight]);

% reset to original units for axes and figure
if ~iscell(origaxunits)
  origaxunits = {origaxunits};
end

for i = 1:numel(hax)
  set(hax(i), 'Units', origaxunits{i});
end

set(hfig, 'Units', origfigunits);

%      set(hfig, 'WindowStyle', origwindowstyle);

end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant