From ec7de9fbec535797d46d84966ac026658f76bf6b Mon Sep 17 00:00:00 2001 From: Benjamin K Date: Fri, 14 Feb 2025 11:39:56 +0100 Subject: [PATCH] Update warning message to help windows users --- README.md | 13 ++++++++++++- src/engine.jl | 26 +++++++++++++++++++++----- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d9dd41e..bdc2eb0 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ This package is composed of two aspects: ### Windows -1. For Matlab R2020a onwards, you should be able to go directly to step 2. If you encounter issues, run `matlab -batch "comserver('register')"` in the command prompt. For earlier versions of Matlab, start a command prompt as an administrator and enter `matlab /regserver`. +1. For Matlab R2020a onwards, you should be able to go directly to step 2. If you encounter issues, run `matlab -batch "comserver('register')"` in the command prompt. For earlier versions of Matlab, start a command prompt as an administrator and enter `matlab /regserver`. Alternatively, you can do the same using the MATLAB GUI. To do this, open MATLAB with administrative privileges and run the following command in the MATLAB command window: `!matlab -regserver`. Close MATLAB and restart Julia. 2. From Julia run: `Pkg.add("MATLAB")` @@ -74,6 +74,17 @@ If you had the package `MATLAB.jl` already installed and built before changing t julia> using Pkg; Pkg.build("MATLAB") ``` +### Notes for Windows Users + +If you are using Windows, MATLAB needs to register its COM interface to properly work with MATLAB.jl. This should happen automatically during installation, but if you encounter issues, you can manually re-register MATLAB (with the version you want to use). To do this + +1. Open a MATLAB window with administrative privileges +2. Run the following command in the MATLAB command window: `!matlab -regserver` +3. Close MATLAB and restart Julia + +> [!IMPORTANT] +> The version of MATLAB that is registered must be same that `MATLAB.jl` uses. + ## Usage diff --git a/src/engine.jl b/src/engine.jl index f42b6a1..f61f868 100644 --- a/src/engine.jl +++ b/src/engine.jl @@ -16,6 +16,21 @@ startcmd(flags::AbstractVector{<:AbstractString}) = # 64 K buffer should be sufficient to store the output text in most cases const default_output_buffer_size = 64 * 1024 +const windows_regserver_warning = """ +Failed to start MATLAB engine. If you have/had multiple versions of MATLAB installed, this can happen if you +tried to start a different version of MATLAB in Julia compared to which MATLAB server is registered in Windows. + +Steps to resolve this: + +1. Register a specific MATLAB version manually as a server, open a MATLAB window as a user with administrator privileges. +In MATLAB, enter the command `!matlab -regserver`. Then close the MATLAB window. More details: +https://de.mathworks.com/help/matlab/matlab_external/registering-matlab-software-as-a-com-server.html + +2. Ensure that the MATLAB.jl package is using the same MATLAB version that was registered in step 1. See the instructions on GitHub +on how to change the version that MATLAB.jl uses: +https://github.com/JuliaInterop/MATLAB.jl?tab=readme-ov-file#changing-matlab-version +""" + mutable struct MSession ptr::Ptr{Cvoid} buffer::Vector{UInt8} @@ -27,12 +42,13 @@ mutable struct MSession end ep = ccall(eng_open[], Ptr{Cvoid}, (Ptr{UInt8},), startcmd(flags)) if ep == C_NULL - @warn("Confirm MATLAB is installed and discoverable.", maxlog = 1) + @warn( + "Confirm MATLAB is installed and discoverable.", + matlab_libpath, + maxlog = 1 + ) if Sys.iswindows() - @warn( - "Ensure `matlab -regserver` has been run in a Command Prompt as Administrator.", - maxlog = 1 - ) + @warn(windows_regserver_warning, maxlog = 1) elseif Sys.islinux() @warn( "Ensure `csh` is installed; this may require running `sudo apt-get install csh`.",