Skip to content

Commit 9ccdd3d

Browse files
authored
Merge pull request #105 from musm/w
Clean up show/hide/and add get visibility method on Windows
2 parents 3843945 + a2a4f2b commit 9ccdd3d

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,26 @@ xx, yy = mxcall(:meshgrid, 2, x, y)
311311

312312
``mxcall`` puts the input arguments to the MATLAB workspace (using mangled names), evaluates the function call in MATLAB, and retrievs the variable from the MATLAB session. This function is mainly provided for convenience. However, you should keep in mind that it may incur considerable overhead due to the communication between MATLAB and Julia domain.
313313

314-
#### Viewing the MATLAB Session
314+
#### Viewing the MATLAB Session (Windows only)
315315

316-
To open an interactive window for the MATLAB session, use the command `show_msession()`. To hide the window, use `hide_msession()`. Note that closing this window will result in an error, so it is advised that one uses the `hide_msession()` command instead.
316+
To open an interactive window for the MATLAB session, use the command `show_msession()` and to hide the window, use `hide_msession()`. *Warning: manually closing this window will result in an error or result in a segfault; it is advised that you only use the `hide_msession()` command to hide the interactive window.*
317317

318318
Note that this feature only works on Windows.
319319

320+
```julia
321+
# default
322+
show_msession() # open the default MATLAB session interactive window
323+
get_msession_visiblity() # get the session's visibility state
324+
hide_msession() # hide the default MATLAB session interactive window
325+
326+
# similarily
327+
s = MSession()
328+
show_msession(s)
329+
get_msession_visiblity(a)
330+
hide_msession(s)
331+
```
332+
333+
320334
#### Advanced use of MATLAB Engines
321335

322336
This package provides a series of functions for users to control the communication with MATLAB sessions.

src/MATLAB.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ export MSession, MatFile,
3131
eval_string, get_mvariable, get_variable, put_variable, put_variables,
3232
variable_names, read_matfile, write_matfile,
3333
mxcall,
34-
@mput, @mget, @matlab, @mat_str,
35-
show_msession, hide_msession
34+
@mput, @mget, @matlab, @mat_str
3635

36+
if is_windows()
37+
export show_msession, hide_msession, get_msession_visiblity
38+
end
3739

3840
# exceptions
3941
type MEngineError <: Exception
@@ -65,6 +67,7 @@ function __init__()
6567
eng_open[] = engfunc(:engOpen)
6668
eng_close[] = engfunc(:engClose)
6769
eng_set_visible[] = engfunc(:engSetVisible)
70+
eng_get_visible[] = engfunc(:engGetVisible)
6871
eng_output_buffer[] = engfunc(:engOutputBuffer)
6972
eng_eval_string[] = engfunc(:engEvalString)
7073
eng_put_variable[] = engfunc(:engPutVariable)

src/engine.jl

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,25 @@ function close_default_msession()
9090
return nothing
9191
end
9292

93-
function show_msession(m::MSession = get_default_msession())
94-
ret = ccall(eng_set_visible[], Cint, (Ptr{Void}, Cint), m, 1)
95-
ret != 0 && throw(MEngineError("failed to show MATLAB engine session (err = $ret)"))
96-
return nothing
97-
end
93+
if is_windows()
94+
function show_msession(m::MSession = get_default_msession())
95+
ret = ccall(eng_set_visible[], Cint, (Ptr{Void}, Cint), m, 1)
96+
ret != 0 && throw(MEngineError("failed to show MATLAB engine session (err = $ret)"))
97+
return nothing
98+
end
9899

99-
function hide_msession(m::MSession = get_default_msession())
100-
ret = ccall(eng_set_visible[], Cint, (Ptr{Void}, Cint), m, 0)
101-
ret != 0 && throw(MEngineError("failed to hide MATLAB engine session (err = $ret)"))
102-
return nothing
103-
end
100+
function hide_msession(m::MSession = get_default_msession())
101+
ret = ccall(eng_set_visible[], Cint, (Ptr{Void}, Cint), m, 0)
102+
ret != 0 && throw(MEngineError("failed to hide MATLAB engine session (err = $ret)"))
103+
return nothing
104+
end
104105

106+
function get_msession_visiblity(m::MSession = get_default_msession())
107+
vis = Ref{Cint}(true)
108+
ccall(eng_get_visible[], Int, (Ptr{Void}, Ptr{Cint}), m, vis)
109+
return vis[] == 1 ? true : false
110+
end
111+
end
105112

106113
###########################################################
107114
#

src/init.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const libmat = Ref{Ptr{Void}}()
99
const eng_open = Ref{Ptr{Void}}()
1010
const eng_close = Ref{Ptr{Void}}()
1111
const eng_set_visible = Ref{Ptr{Void}}()
12+
const eng_get_visible = Ref{Ptr{Void}}()
1213
const eng_output_buffer = Ref{Ptr{Void}}()
1314
const eng_eval_string = Ref{Ptr{Void}}()
1415
const eng_put_variable = Ref{Ptr{Void}}()

0 commit comments

Comments
 (0)