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.

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)
  if(Matlab_FOUND)
    add_test(NAME matlab-${v} COMMAND ${Matlab_MAIN_PROGRAM} -batch "...")
  endif()
endforeach()