Scientific Computing

Windows Hibernate enable / disable

Across operating systems, “hibernate” is a power-saving state that saves the current session to disk and powers down the computer, allowing for a quick resume later while not having the long-term battery drain of suspend mode that keeps the RAM powered. Sometimes as part of diagnosing problems or ensuring that a remote computer stays powered on, hibernate might be disabled. If restoring such a computer to personal use, or if experiencing issues where a laptop computer has a drained battery after a couple days of non-use, it may be useful to enable hibernate mode.

Check the current state of hibernate mode by running the following command in Windows Terminal:

powercfg /availablesleepstates

If hibernate is enabled, the output will include “Hibernate” in the list of available sleep states. To enable hibernate mode:

powercfg /hibernate on

However, the option in Control Panel → Power Options → Change plan settings → Change advanced power settings → Sleep → “Hibernate after” may not be available until making the following registry change:

REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Power\PowerSettings\238C9FA8-0AAD-41ED-83F4-97BE242C8F20\9d7815a6-7ee4-497e-8888-515a05f02364 /v Attributes /t REG_DWORD /d 2 /f

Reboot.

Enable the hibernate timer setting, where the values are in minutes:

powercfg /setACvalueindex scheme_current SUB_SLEEP HIBERNATEIDLE 43200
# 12 hours hibernate while plugged into AC power (or set to 0 for never)

powercfg /setDCvalueindex scheme_current SUB_SLEEP HIBERNATEIDLE 10800
# 3 hours hibernate while on battery (or set to 0 for never)

powercfg /setactive SCHEME_CURRENT
# Set the current power scheme to apply changes

Matlab upgrade release

Matlab version upgrade and AddOns installer was significantly changed in Matlab R2025a. Matlab release upgrades can be initiated from the “bell” icon or from the Matlab Help menu. Upgrading or Add-On install requires a graphical connection to the computer (Remote Desktop, VNC, or similar). Offline installs are considerably more complex and are not for the typical Matlab user.

Matlab release upgrade

The bell icon on the upper right corner of the Matlab Desktop typically shows when a Matlab update is available. Matlab can force checking for update, even if the bell is not showing an update, with our script MatlabReleaseUpgrade.m.

Matlab Add-Ons and mpm CLI installer

Matlab Add-On Explorer is launched from the Matlab Desktop and requires a graphical connection. The mpm CLI installer can be used to install Matlab and Add-Ons from the command line.

Troubleshooting

If the graphical connection has a problem like:

terminating with uncaught exception of type (anonymous namespace)::DisplayError: No display available.

try making a graphical UI window like:

h = uihtml(HTMLSource=which('peppers.png'), Position=[0,0,512,384])

This should show the Peppers image including with Matlab.


If on Linux with a graphical connection and errors result, try prioritizing system libcrypto.so like:

LD_PRELOAD=/path/to/libcrypto.so matlab

Related: Matlab install on Linux

Matlab on macOS doesn't source shell config

On macOS, Matlab does not source any shell configuration files like ~/.zshrc or ~/.bashrc. Any environment variables set in these files, such as those added by package managers like Homebrew, will not be available in the Matlab environment.

Instead, set desired environment variables in the Matlab startup.m script like:

edit(fullfile(userpath,'startup.m'))

Adding lines like

setenv("PATH", ['/opt/homebrew/bin/', pathsep, getenv("PATH")])

Where “/opt/homebrew/bin” was obtained from the system command brew --prefix. We recommend not running system() scripts in startup.m unless truly necessary to avoid delaying Matlab startup time or affecting startup stability.

Then programs installed by Homebrew like CMake, GCC, etc. will be on Path environment variable in Matlab.

Note that the Matlab commands below only affect commands withing that same command line:

!source ~/.zshrc && ls
system("source ~/.zshrc && ls")

Fortran MPI on Windows

Get MPI on Windows with C and Fortran by any one of: MS-MPI, Intel oneAPI or Windows Subsystem for Linux libopenmpi-dev. We often use MPI via MSYS2 with GCC / Gfortran compilers on Windows.

The “mpi_f08” interface is not yet available via MS-MPI.

Install Microsoft MPI:

winget install Microsoft.msmpi

Install MSYS2 MS-MPI library from the MSYS2 Terminal:

pacman -S mingw-w64-ucrt-x86_64-msmpi

OpenMPI is not currently available for native Windows. MPICH is not directly buildable on Windows, even with MSYS2.

Upon installing or updating Intel oneAPI compilers and libraries on Windows, you may experience CMake failing to find MPI for MinGW. This happens because Intel compilers put Intel MPI on the system PATH. Fix this by removing Intel MPI from the system PATH and use the Intel Compiler shell instead, which provides all the needed directories.

Notepad++ multi-window shortcut

These techniques allow a Windows development machine to use Notepad++ more effectively. Notepad++ is a free source code editor and Notepad. It was first released in 2003 and is an open source project.

winget install Notepad++.Notepad++
  • Notepad++ from command line: add to user environment variable Path the binary path of Notepad++ – typically at %PROGRAMFILES%\Notepad++. This allows using notepad++ from the Windows Terminal.

  • Notepad++ shortcut icon open a new window: When using multiple virtual desktops, by default Notepad++ snaps to the other desktop when clicking the Notepad++ start menu icon. Make the Notepad++ icon open a new Notepad++ window by editing the Notepad++ start menu shortcut, appending -multiInst to the right and outside of the quoted full Target path to notepad++.exe. This allows opening multiple Notepad++ windows, each with its own set of tabs.

  • Notepad++ open directory as project: To open a directory as a project, use the command line option -openFoldersAsWorkspace followed by the path to the directory.

notepad++ -openFoldersAsWorkspace "C:\path\to\directory"

opens that directory in Notepad++ as a hierarchical workspace rather than opening every individual file.

SSH authentication encryption selection

SSH has deprecated the arcfour-hmac encryption type for authentication. Upon logging into a server, the warning may emit:

Warning: encryption type arcfour-hmac used for authentication is deprecated and will be disabled

Check the SSH client configuration per-user file: “$HOME/.ssh/config”. There is also a system-wide SSH client configuration file that can be edited:

  • Linux & macOS: /etc/ssh/ssh_config
  • Windows: $Env:ProgramData/ssh/ssh_config

We have found this is often a server-side configuration issue, particularly when it only occurs with a specific server. The server may be using an older version of OpenSSH that still supports arcfour-hmac, or it may have been configured to allow this encryption type.

To check settings in the SSH client configuration file, look for lines that specify Ciphers or MACs. If arcfour-hmac is listed, it should be removed.

Matlab Python JIT Bug

The Matlab external language interface to Python in Matlab releases before R2022a has a bug with the JIT compilation via Matlab execution engine such that any mention of the py.* namespace will make a function unusable if Python is not available. Especially problematic is functions that can work without Python (say in an if-else or switch block) where the mere presence of a py.* call will cause the entire function to fail if Python is not available.

To workaround this issue in Matlab older than R2022a, use a private function to encapsulate the Python call. This way, the main function will not be affected by the Python call.

function result = my_fun2(input)

  if has_python()
    result = encaps_fun2(input);
  else
    % fallback code when Python is not available
  end

end

Make a private function in file “private/encaps_fun2.m”:

function result = encaps_fun2(input)
    result = py.some_module.some_function(input);
end

Matlab .NET on Linux, macOS, and Windows

Matlab external language interfaces includes .NET on Windows, Linux, and macOS. This allows efficiently calling .NET assemblies and using .NET libraries directly from Matlab.

The Matlab function dotnetenv is used to set up and check the active .NET environment in Matlab. Environment variable DOTNET_ROOT is vital for Matlab to detect the .NET installation, particularly on Linux and macOS. If Matlab is having issues detecting the .NET installation NET.isNETSupported is false, determine the value for DOTNET_ROOT from system Terminal:

dotnet --info

If “dotnet” command is not found, install .NET SDK:

  • macOS: brew install dotnet
  • Windows: winget install Microsoft.DotNet.SDK.9 # whatever the desired version is
  • Linux: from Microsoft
  • Matlab Online (Linux): if .NET isn’t detected, use .NET install script to install .NET SDK.

Find the “dotnet” executable and run dotnet --info to get the DOTNET_ROOT path. If this path is not defined, look for the Base Path, and pick the directory that contains the “dotnet” executable.

Set the DOTNET_ROOT environment variable in Matlab by adding to Matlab startup.m so that future Matlab sessions have the correct DOTNET_ROOT set.

edit(fullfile(userpath,'startup.m'))

Then add the following line to startup.m:

setenv("DOTNET_ROOT", <DOTNET_ROOT path from dotnet --info>)

Restart Matlab. Do the following one-time command to finish setting up .NET in Matlab:

dotnetenv("core", Version=8)

The Version number must match the major version of .NET in DOTNET_ROOT. On future Matlab sessions, dotnetenv() will be empty until the first .NET command is run, for example

NET.isNETSupported

then (optionally) running dotnetenv() shows the current .NET environment.

Detect if Python running as sudo

On Unix-like systems such as Linux and macOS, Python can detect if a script is being run as sudo / root from the UID, which is by definition zero for root.

On Windows, the ctypes module can be used to check if the current user is an administrator.

import os
import ctypes

def running_as_root() -> bool:
    if os.name == 'nt':
        return ctypes.windll.shell32.IsUserAnAdmin()
    else:
        return os.getuid() == 0

Flip image using command line

Its convenient to flip an image vertically or horizontally from Terminal. In general, modifying an image in any way including cropping, flipping, rotating, etc. is a lossy process if the image compression algorithm used is lossy such as JPEG. Lossless compression formats such as PNG will remain lossless with this operation.

Here are examples using ImageMagick or IrfanView. Assume the input image is named “in.png” and the modified image is written to “out.png”.

ImageMagick

ImageMagick is available on most operating systems via their package managers.

  • Windows: winget install ImageMagick.ImageMagick WinGet auto adds “magick” to PATH after restarting Terminal
  • macOS: brew install imagemagick
  • Linux: apt install imagemagick

The order of the options is very important in ImageMagick.

Vertical flip the image by:

magick in.png -flip out.png

Horizontal flip the image by:

magick in.png -flop out.png

IrfanView

IrfanView has been popular for decades on Windows and WINE as a no-cost (not open source) image viewing and simple modification program that handles many formats. IrfanView can also be used from the command line. Install IrfanView directly, not via the Microsoft App Store:

winget install IrfanSkiljan.IrfanView

The order of the commands is important. These commands do not open the IrfanView GUI–they are “headless” operations. If there are spaces in the file paths, they must be enclosed in quotes like "c:/data pics/in.png"

Vertical flip the image by:

& $Env:ProgramFiles\IrfanView\i_view64 in.png /vflip /convert=out.png

Horizontal flip the image by:

& $Env:ProgramFiles\IrfanView\i_view64 in.png /hflip /convert=out.png