chonglim
chonglim

Reputation: 33

Turn off window screen using Matlab function

I have a code in which I want to turn off the computer screen using a Matlab function. I did find code that shuts down the computer via system('shutdown -s'), but I just need to turn the display of my laptop off. I am using Window 7. Could anyone suggest how?

Upvotes: 0

Views: 374

Answers (1)

hbaderts
hbaderts

Reputation: 14316

This is highly OS-specific. Here are a few possibilities:

Mac OS X: From OS X 10.9 on, there is the command pmset displaysleepnow which you can call in the terminal. To call this from MATLAB, simply use

system('pmset displaysleepnow');

Linux: In Linux (I couldn't test that yet though) you can use xset dpms to control the screen. You might have to activate dpms first with xset +dpms, then you can use

system('xset dpms force off');

Windows: The easiest way to get this to work on Windows is by installing the software NirCmd which allows you to control several Windows properties from the command line. Then turning the screen off is as easy as

system('nircmd.exe monitor off');

As this requires external software, here's another idea: You can use SendMessage to switch the screen off. For that you'll have to create a small C or C++ program. You can either write a separate tool to call via system, or write a MEX file to do that. I'd suggest a MEX file like this:

#include <windows.h>   // needed for SendMessage
#include <mex.h>       // needed for MEX files

void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
    SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
}

Note: I haven't tested this solution yet, will do that as soon as I have a computer with MATLAB on Windows.

Upvotes: 1

Related Questions