Matlab version selection with CMake

NOTE: CMake 3.19 enabled the ability to use find_package with a version range, making the method below unnecessary.

Many of our Matlab codes require a modern version of Matlab. There may also be beta Matlab versions we want to avoid with known bugs. It’s possible to select from an arbitrary range of Matlab versions with CMake FindMatlab as follows.

This list selects from Matlab R2020b (9.9) down to R2019a (9.6). You can put Matlab versions in any order you like.

If you want to run the test only for the first Matlab version found in the list, insert break() right after add_test.

foreach(v 9.9 9.8 9.7 9.6)
  find_package(Matlab ${v} EXACT COMPONENTS MAIN_PROGRAM)
    add_test(NAME matlab-${v} COMMAND ${Matlab_MAIN_PROGRAM} -batch "...")