Matlab scripting quick tip: Dual monitor setup

I have been using Matlab for almost all of my computing needs. It is very easy to program especially when dealing with matrices. It has also a rich library of graphical interface. You can easily build a highly interactive graphical user interface using its GUI development environment or GUIDE. If you are familiar with event-based programming, you will have no difficulty using GUIDE.

Here is a quick tip you might find useful when programming in Matlab.

Dual-Monitor Setup

If you have a dual-monitor setup (using two monitors to extend the screen) and wanted to display all graphics-related output in the other monitor, you can use the ‘MonitorPositions’ property of the root window (0) to get the coordinates and dimensions of the two monitors as seen by Matlab. The code will look like this:

>> pos = get(0,'MonitorPositions');

If Matlab detects your dual-monitor setup, pos is a 2×4 matrix with the first row corresponding to the coordinates and dimension of your first monitor and the 2nd row that of the second monitor. The first two elements is the x and y position, while the last two correspond to the width and height. So for example, to position your figure in the second monitor in full screen, you can simply call the figure command with the position property set to pos(2,:), that is,

>> hfig = figure('Position',pos(2,:));

To make your application robust, you must first check if you have a second monitor before using the above code or else you will get an ‘Index exceeds matrix dimensions‘ error. You can accomplish this using the following code:

pos = get(0,'MonitorPositions');
sz = size(pos);
if (sz(1) > 1)
    hfig = figure('Position',pos(2,:));
    hfig = figure('Position',pos);

This will display your figure window full screen in the second monitor, if you have one. If you don’t, this will still display the figure, full screen, in the current monitor. And that’s it.

You may also like...

1 Response

  1. Ajasja says:


    pos = get(0,’MonitorPositions’);

    also work?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.