Skip to content

Commit 348452a

Browse files
authored
Better error for failed MEngine session (#118)
1 parent 8fbc071 commit 348452a

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/engine.jl

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ mutable struct MSession
1717

1818
function MSession(bufsize::Integer = default_output_buffer_size)
1919
ep = ccall(eng_open[], Ptr{Void}, (Ptr{UInt8},), default_startcmd)
20-
ep == C_NULL && throw(MEngineError("failed to open a MATLAB engine session"))
20+
if ep == C_NULL
21+
Base.warn_once("Confirm MATLAB is installed and discoverable.")
22+
if iswindows()
23+
Base.warn_once("Ensure `matlab -regserver` has been run in a Command Prompt as Administrator.")
24+
elseif islinux()
25+
Base.warn_once("Ensure `csh` is installed; this may require running `sudo apt-get install csh`.")
26+
end
27+
throw(MEngineError("failed to open MATLAB engine session"))
28+
end
2129
# hide the MATLAB command window on Windows
2230
iswindows() && ccall(eng_set_visible[], Cint, (Ptr{Void}, Cint), ep, 0)
2331

@@ -54,7 +62,7 @@ end
5462
function close(session::MSession)
5563
# close a MATLAB Engine session
5664
ret = ccall(eng_close[], Cint, (Ptr{Void},), session)
57-
ret != 0 && throw(MEngineError("failed to close a MATLAB engine session (err = $ret)"))
65+
ret != 0 && throw(MEngineError("failed to close MATLAB engine session (err = $ret)"))
5866
session.ptr = C_NULL
5967
return nothing
6068
end
@@ -137,7 +145,7 @@ eval_string(stmt::String) = eval_string(get_default_msession(), stmt)
137145
function put_variable(session::MSession, name::Symbol, v::MxArray)
138146
# put a variable into a MATLAB engine session
139147
ret = ccall(eng_put_variable[], Cint, (Ptr{Void}, Ptr{UInt8}, Ptr{Void}), session, string(name), v)
140-
ret != 0 && throw(MEngineError("failed to put the variable $(name) into a MATLAB session (err = $ret)"))
148+
ret != 0 && throw(MEngineError("failed to put variable $(name) into MATLAB session (err = $ret)"))
141149
return nothing
142150
end
143151

@@ -148,7 +156,7 @@ put_variable(name::Symbol, v) = put_variable(get_default_msession(), name, v)
148156

149157
function get_mvariable(session::MSession, name::Symbol)
150158
pv = ccall(eng_get_variable[], Ptr{Void}, (Ptr{Void}, Ptr{UInt8}), session, string(name))
151-
pv == C_NULL && throw(MEngineError("failed to get the variable $(name) from a MATLAB session"))
159+
pv == C_NULL && throw(MEngineError("failed to get variable $(name) from MATLAB session"))
152160
return MxArray(pv)
153161
end
154162

0 commit comments

Comments
 (0)