Search code examples
matlabmatlab-figuremultiple-monitors

How can I control which monitor plots are displayed on?


I have a 3 monitor Gentoo Linux system running MATLAB. MATLAB runs on the center monitor. I need MATLAB to generate plots on the left monitor but it always plots on the right monitor.

I believe this is at least partially caused by the non-standard way I have my monitors arranged physically - essentially 2,3,1:

>> get(0,'MonitorPositions')

ans =

           1           1        1920        1080
       -3839           1        1920        1080
       -1919           1        1920        1080

Is there a way I can control this as a default within MATLAB?


Solution

  • You can set the default figure position on the root object like so:

    set(0, 'DefaultFigurePosition', [-3839 1 1920 1080]);
    

    This will create windows that fill the left monitor by default. However, this default will likely reset each time you restart MATLAB, so you will have to put it in your startup file if you want it to persist from session to session.

    Note: The documentation for the 'MonitorPositions' property of the root object says this:

    The first two elements in each row indicate the display location with respect to the origin point. The last two elements in each row indicate the display size. The origin point is the lower-left corner of the primary display.

    If you change which monitor is used as the primary display, the relative coordinates in the left two columns will change, meaning you will have to change the position value in the above line of code. If you think the display setup may change often, or you will be running code on different monitor setups, then you can ensure plots will always appear on the left-most monitor by looking for the monitor position with the lowest value in the left column. Here's how you could do it (also incorporating the previous default window size and position within a monitor):

    monitorPos = get(0, 'MonitorPositions');
    figurePos = get(0, 'DefaultFigurePosition');
    [~, leftIndex] = min(monitorPos(:, 1));
    set(0, 'DefaultFigurePosition', figurePos + [monitorPos(leftIndex, 1:2)-1 0 0]);