Matlab version selection with CMake

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 R2020a (9.8) 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.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()