Search code examples
matlablan

MATLAB: Disable network adapter


I'm using an eye tracking device with MATLAB. Having an active internet connection whilst performing eye tracking experiments can cause data dropout. At present, I manually disable my network adapter before beginning any experiments. I wonder if this can be acheived using a MATLAB command? This is the only active computer in my lab (from which I am writing this message right now) so it would be nice to have the network connection automatically disabled when I start running a program, and for it to re-enable itself following an experiment. I'm not aware of any command within MATLAB that would allow me to take such control.

P.S. This computer is running Windows XP and the connection I am trying to access is a LAN connection. The eye tracker itself is connected via another LAN connection, so disabling all network adapters is not a viable solution.


Solution

  • A couple of system commands to disable/enable network adapter. Remember you need administrative privileges to do it.

    netsh interface set interface "<InterfaceName>" DISABLED
    
    netsh interface set interface "<InterfaceName>" ENABLED
    

    where "<InterfaceName>" is for example "Local Area Network".

    Alternatively using WMIC

    wmic path win32_networkadapter where index=7 call disable
    
    wmic path win32_networkadapter where index=7 call enable
    

    To find index

    wmic nic get name, index
    

    You can run those commands from MATLAB by system function as in @ypnos answer.

    [status, result] = system('command')
    

    You may need to add RUNAS command to run as administrator.