Scientific Computing

GNU Octave for continuous integration

Matlab CI is often a better choice than Octave below.


Cross-platform developers run into numerous compatibility issues. Rather than wait for frustrated users to report such a bug, use continuous integration.

Here are CI templates using GNU Octave tests of .m code. Octave oruntests() is incompatible with the advanced functionality of Matlab runtests(),

GitHub Actions: “.github/workflows/ci.yml”:

name: ci

on:
  push:
    paths:
    - "**.m"
    - ".github/workflows/ci.yml"

jobs:

  linux:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout

    - run: |
        sudo apt update
        sudo apt install --no-install-recommends octave

    - run: octave --eval "test_myfuncs"
      working-directory: tests

  windows:
    runs-on: windows-2025
    steps:
    - uses: actions/checkout
    - run: winget install GNU.Octave --disable-interactivity --accept-source-agreements --accept-package-agreements

    - run: octave --eval "test_myfuncs"
      working-directory: tests

GitHub Actions winget install

WinGet can be used on Windows GitHub Actions runners where a Windows program needs to be installed. In this example, environment variable FFMPEG_ROOT tells Python where to find the ffmpeg.exe program. One could more generally append to the GITHUB_PATH environment variable.


jobs:
  windows:
    runs-on: windows-2025

    steps:

    - name: install prereqs (Windows)
      if: runner.os == 'Windows'
      run: winget install ffmpeg --disable-interactivity --accept-source-agreements --accept-package-agreements

    - name: FFMPEG_ROOT Windows
      if: runner.os == 'Windows'
      run: echo "FFMPEG_ROOT=$env:LOCALAPPDATA/Microsoft/WinGet/Links/" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append

    - name: PyTest
      run: pytest
import functools
import shutil
import os


@functools.cache
def get_exe(name: str) -> str:

    for p in (os.environ.get("FFMPEG_ROOT"), None):
        if exe := shutil.which(name, path=p):
            return exe

    raise FileNotFoundError(name)

Disable Installed Addons in MATLAB

Matlab Addons are packages / toolboxes that can be installed into Matlab. Addons may come from the Mathworks or third-party developers. One can disable non-Mathworks addons to ensure that the project code works as expected in diverse environments.

Check Matlab Addon packages installed:

addons = matlab.addons.installedAddons;

disp(addons)

Disable a non-Mathworks addon by the matlab.addons.disableAddon function. It isn’t possible to disable Mathworks addons; the following results:

Enable/Disable not supported for MathWorks products, MathWorks toolboxes, or support packages.

Check logical status of the addon:

matlab.addons.isAddonEnabled("MyCustomAddon")

Alert boxes in Hugo site generator

If alert boxes (like “note”, “warning”) are not built into a site’s Hugo theme, they can be added with a bit of CSS and partial template.

Edit / add to the Hugo site as in this Gist to enable alert boxes.

Prefer IPv4 for conda on Windows

Windows users generally should not disable IPv6. Upon network upgrades or working in a new location with IPv6, network operations that previously worked may fail. An example of this is “conda install” or “conda update” commands that hang or fail with a timeout error. While curl has an option “-4” or “–ipv4” to force IPv4 connections only, the “conda” command does not have a “force IPv4” option currently. Windows can be set to prioritize IPv4 over IPv6, which can help with conda operations and other network operations. Reprioritizing IPv4 over IPv6 is vastly preferable to disabling IPv6, as it allows for compatibility with IPv6 networks.

Check existing IPv6 settings with the command:

netsh interface ipv6 show prefixpolicies

ℹ️ Note

If there are only one or zero prefix policy entries, then IPv6 is likely not configured correctly. The rest of this procedure would not help as Windows will go to factory defaults and ignore the IPv4 preference. Fix the IPv6 configuration first, then proceed with the steps below.

Set the prefix policy for IPv4 to have a higher priority than IPv6 by running the following command in an elevated PowerShell or Command Prompt:

netsh interface ipv6 set prefixpolicy ::ffff:0:0/96 46 4

Check that the IPv6 prefix policy has been set correctly by running:

netsh interface ipv6 show prefixpolicies

Then execute the “conda install” or “conda update” command again.


To restore the default Windows settings of IPv6 having priority over IPv4:

netsh interface ipv6 set prefixpolicy ::ffff:0:0/96 35 4

Matlab arguments validation

Matlab function arguments validation syntax is generally recommended over validateattributes() and inputParser(). Function arguments validation specification coerces the input and/or output variables to the class declaration given if possible, and errors otherwise.

  • Default values are easily specified, which required such verbose syntax before.
  • only a single class can be specified
  • recall the .empty method of most Matlab classes e.g. datetime.empty() that allows initializing an empty array.

Matlab argument validation syntax coerces class at runtime.

GNU Octave compatibility

Currently GNU Octave does not enable function arguments validation syntax, but it is possible to use the function albeit with warnings like:

“function arguments validation blocks are not supported; INCORRECT RESULTS ARE POSSIBLE”

To elide the verbose warnings to allow for self-tests or quick use, do in Octave like:

warning('off', 'all')

% whatever code you want to run that uses the Matlab arguments validation syntax
oruntests .

% restore warnings
warning('on', 'all')

There are requests for Octave to assign a warning ID so that only this warning can be silenced, but it is not implemented as of this writing. Thus all warnings are silenced, which is not recommended in production code. However, for self-tests or quick use, it could be acceptable.

Intel oneAPI on GitHub Actions

Intel oneAPI is a useful, performant compiler for CI with the C, C++, and Fortran languages. oneAPI compilers give useful debugging output at build and run time. If having trouble determining the oneAPI APT package name, examine the APT repo Packages file on the developer computer like:

curl -O https://apt.repos.intel.com/oneapi/dists/all/main/binary-amd64/Packages.gz

gunzip Packages.gz

less Packages

This examples oneAPI GitHub Actions workflow works for C, C++, and Fortran.

Persistent user paths to Matlab and Octave

Matlab .mltbx toolbox packaged toolbox format is proprietary to Matlab. Oftentimes we desire to distribute a Matlab package as a set of .m source files instead. To add a “toolbox” or “package” to Matlab and use functions from that toolbox requires using “addpath” or “import” Matlab syntax. This example makes those paths persistent in Matlab and Octave, using example toolbox directories ~/mypkg1 and ~/mypkg2.

Normally use addpath() instead of cd(). Do not put brackets or braces around the multiple paths.

Prepend a package to the Matlab path by editing the startup.m file from Matlab or Octave:

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

put in addpath() commands to the desired Matlab packages paths like:

addpath('~/mypkg1','~/mypkg2')

Restart Matlab/Octave and type

path

and the new toolbox directories will be at the top.

Matlab MEX compiler setup

Matlab requires compilers for mex -setup langage used (C / C++ / Fortran) and Matlab Engine for the respective code language. Windows Matlab supported compiler locations are communicated to Matlab via environment variables.

One-time setup MEX:

mex -setup C
mex -setup C++
mex -setup Fortran

Inspect Matlab MEX parameters:

mex.getCompilerConfigurations('c')
mex.getCompilerConfigurations('c++')
mex.getCompilerConfigurations('fortran')

It’s possible to switch between compilers that are setup with MEX. Choosing compilers is generally not possible on Linux or macOS from within Matlab. If a oneAPI version compatible with Matlab is installed on Windows, Matlab may detect it and allow switching compilers. If a different compiler is detected and allowed by Matlab, commands to choose the compiler will be at the bottom of the output when using the “mex -setup” commands below.

If having trouble with mex -setup for example if setup fails on macOS like:

“sh: /var/folders/…/mex_…: No such file or directory”

Try running the mex -setup command from Terminal using Matlab batch mode to see if Matlab’s shell was breaking setup. This usually fixes the setup issue. In our cases, we found that environment variables MATLAB_SHELL and SHELL were already set appropriately (not the generic /bin/sh), but we still had to run the mex -setup command from Terminal.

matlab -batch "mex -setup C -v"
matlab -batch "mex -setup C++ -v"
matlab -batch "mex -setup Fortran -v"

Once MEX is working, consider using Matlab buildtool build system for simple, terse syntax to build and test MEX and Matlab Engine code.

Windows MinGW MEX

Using MinGW on Windows with Matlab requires having an exact version of MinGW supported by Matlab. For example, the version of MinGW with MSYS2 is generally not supported by Matlab.

Tell Matlab the supported MinGW compiler path via Windows environment variable MW_MINGW64_LOC.

Find the MinGW compiler location from PowerShell:

(Get-Item((Get-Command gfortran.exe).Path)).Directory.parent.FullName

This would be something like “C:\msys64\ucrt64”.

Put that path in Matlab:

setenv('MW_MINGW64_LOC', '<path of gcc.exe>')

Do “mex -setup” as above.

C MEX Example

Compile built-in C example

mex(fullfile(matlabroot,'extern/examples/mex/yprime.c'))

Run

yprime(3, [4,2,7,1])

ans = 2.0000 5.9924 1.0000 2.986

Fortran MEX example

Compile built-in Fortran example:

mex(fullfile(matlabroot,'extern/examples/refbook/timestwo.F'))

Run:

timestwo(3)

ans = 6.0