Search code examples
matlabpositionmonitorfigure

Modify autoArrangeFigures (from Matlab File Exchange) to send different plots to different monitors


Using

autoArrangeFigures(0,0,2); % (0,0,x) where x is monitor ID

one can choose where all figures are to be placed. I would like, however, to control which figures go to which monitor.

MWE attempt:

close all; clear all; clc

% make 10 figures
for i=1:10
    figure()
end

autoArrangeFigures(0,0,2); % (0,0,x) where x is monitor ID

pause(2)

% make 10 figures
for i=1:10
    figure()
end

autoArrangeFigures(0,0,1); % (0,0,x) where x is monitor ID

This only redirects all 20 figures to new monitor position. It doesn't keep the 10 figure positions from the first call of autoArrangeFigures(0,0,2) in monitor 2, but redirects all 10+10 figures to monitor 1 via autoArrangeFigures(0,0,1).

How to fix this?

autoArrangeFigures can be found at:

https://se.mathworks.com/matlabcentral/fileexchange/48480-automatically-arrange-figure-windows


Solution

  • One thing that you could do is modify the autoArrangeFigures function to take an additional (optional) argument figHandles.

    Modify the first line to:

    function autoArrangeFigures(NH, NW, monitor_id, figHandle)
    

    To make this argument optional, you can change line 39

    figHandle = sortFigureHandles(findobj('Type','figure'));
    

    into

    if ~exist('figHandle', 'var') || isempty(figHandle)
        figHandle = sortFigureHandles(findobj('Type','figure'));
    else
        figHandle = figHandle(:); % make row vector
    end
    

    So then your MWE would look like:

    clear fig
    % make 10 figures
    for i=1:10
        fig(i) = figure();
    end
    autoArrangeFigures(0,0,2, fig); % (0,0,x) where x is monitor ID
    
    % make 10 figures
    for i=1:10
        fig(10+i) = figure();
    end
    autoArrangeFigures(0,0,1,fig(11:20)); % (0,0,x) where x is monitor ID
    

    Personally, I am a fan of distFig from the Matlab FEX, which allows what you want out of the box.

    Example:

    for i = 1:20
        fig(i) = figure();
    end
    distFig('screen', 'Secondary', 'only', 1:10) % place only fig 1:10 on the secondary screen
    distFig('screen', 'Primary', 'only', 11:20) % place only fig 11:20 on the primary screen