Skip to content

Commit 7642def

Browse files
authored
Minor clean-up of build, readme, and engine code (#190)
* Minor clean up of build, readme, and engine code
1 parent 783a527 commit 7642def

File tree

3 files changed

+45
-56
lines changed

3 files changed

+45
-56
lines changed

README.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Generally, this package is comprised of two aspects:
2323

2424
**Important**: The procedure to setup this package consists of the following steps.
2525

26-
By default, `MATLAB.jl` uses the MATLAB installation with the greatest version number. To specify that a specific MATLAB installation should be used, set the environment variable `MATLAB_HOME`.
26+
By default, `MATLAB.jl` uses the MATLAB installation with the greatest version number. To specify that a specific MATLAB installation should be used, set the environment variable `MATLAB_ROOT`.
2727

2828
### Windows
2929

@@ -34,12 +34,12 @@ By default, `MATLAB.jl` uses the MATLAB installation with the greatest version n
3434

3535
### Linux
3636

37-
1. Make sure ``matlab`` is in executable path.
37+
1. Make sure ``matlab`` is in executable path.
38+
39+
2. Make sure ``csh`` is installed. (Note: MATLAB for Linux relies on ``csh`` to open an engine session.)
3840

39-
2. Make sure ``csh`` is installed. (Note: MATLAB for Linux relies on ``csh`` to open an engine session.)
40-
4141
To install ``csh`` in Debian/Ubuntu/Linux Mint, you may type in the following command in terminal:
42-
42+
4343
```bash
4444
sudo apt-get install csh
4545
```
@@ -66,7 +66,7 @@ One can use the function ``mxarray`` to create MATLAB variables (of type ``MxArr
6666

6767
```julia
6868
mxarray(Float64, n) # creates an n-by-1 MATLAB zero array of double valued type
69-
mxarray(Int32, m, n) # creates an m-by-n MATLAB zero array of int32 valued type
69+
mxarray(Int32, m, n) # creates an m-by-n MATLAB zero array of int32 valued type
7070
mxarray(Bool, m, n) # creates a MATLAB logical array of size m-by-n
7171

7272
mxarray(Float64, (n1, n2, n3)) # creates a MATLAB array of size n1-by-n2-by-n3
@@ -126,7 +126,7 @@ You may access attributes and data of a MATLAB variable through the functions pr
126126
```julia
127127
# suppose x is of type MxArray
128128
nrows(x) # returns number of rows in x
129-
ncols(x) # returns number of columns in x
129+
ncols(x) # returns number of columns in x
130130
nelems(x) # returns number of elements in x
131131
ndims(x) # returns number of dimensions in x
132132
size(x) # returns the size of x as a tuple
@@ -198,7 +198,7 @@ read_matfile(filename) # returns a dictionary that maps each variable name
198198
write_matfile(filename; name1=v1, name2=v2, ...) # writes all variables given in the
199199
# keyword argument list to a MAT file
200200
```
201-
Both ``read_matfile`` and ``write_matfile`` will close the MAT file handle before returning.
201+
Both ``read_matfile`` and ``write_matfile`` will close the MAT file handle before returning.
202202

203203
**Examples:**
204204

@@ -209,11 +209,11 @@ struct S
209209
z::Vector{Float64}
210210
end
211211

212-
write_matfile("test.mat";
213-
a = Int32[1 2 3; 4 5 6],
214-
b = [1.2, 3.4, 5.6, 7.8],
215-
c = [[0.0, 1.0], [1.0, 2.0], [1.0, 2.0, 3.0]],
216-
d = Dict("name"=>"MATLAB", "score"=>100.0),
212+
write_matfile("test.mat";
213+
a = Int32[1 2 3; 4 5 6],
214+
b = [1.2, 3.4, 5.6, 7.8],
215+
c = [[0.0, 1.0], [1.0, 2.0], [1.0, 2.0, 3.0]],
216+
d = Dict("name"=>"MATLAB", "score"=>100.0),
217217
s = "abcde",
218218
ss = [S(1.0, true, [1., 2.]), S(2.0, false, [3., 4.])] )
219219
```
@@ -264,7 +264,7 @@ As with ordinary string literals, you can also interpolate whole Julia expressio
264264

265265
##### `eval_string`
266266

267-
You may also use the `eval_string` function to evaluate MATLAB code as follows
267+
You may also use the `eval_string` function to evaluate MATLAB code as follows
268268
```julia
269269
eval_string("a = sum([1,2,3])")
270270
```
@@ -292,7 +292,7 @@ xx, yy = mxcall(:meshgrid, 2, x, y)
292292

293293
##### `@mget` and `@mput`
294294

295-
The macro `@mget` can be used to extract the value of a MATLAB variable into Julia
295+
The macro `@mget` can be used to extract the value of a MATLAB variable into Julia
296296
```julia
297297
julia> mat"a = 6"
298298
julia> @mget a
@@ -360,4 +360,3 @@ r2 = jarray(r2_mx)
360360
close(s1) # close session s1
361361
close(s2) # close session s2
362362
```
363-

deps/build.jl

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ import Libdl
22

33
const depsfile = joinpath(@__DIR__, "deps.jl")
44

5-
# Determine MATLAB library path and provide facilities to load libraries with
6-
# this path
7-
8-
function find_matlab_homepath()
9-
matlab_home = get(ENV, "MATLAB_HOME", nothing)
10-
if isnothing(matlab_home)
5+
function find_matlab_root()
6+
# Determine MATLAB library path and provide facilities to load libraries with this path
7+
matlab_root = get(ENV, "MATLAB_ROOT",
8+
get(ENV, "MATLAB_HOME", nothing))
9+
if isnothing(matlab_root)
1110
matlab_exe = Sys.which("matlab")
12-
matlab_home = !isnothing(matlab_exe) ? dirname(dirname(matlab_exe)) : nothing
13-
if isnothing(matlab_home)
11+
if !isnothing(matlab_exe)
12+
matlab_root = dirname(dirname(matlab_exe))
13+
else
1414
if Sys.isapple()
1515
default_dir = "/Applications"
1616
if isdir(default_dir)
1717
dirs = readdir(default_dir)
1818
filter!(app -> occursin(r"^MATLAB_R[0-9]+[ab]\.app$", app), dirs)
1919
if !isempty(dirs)
20-
matlab_home = joinpath(default_dir, maximum(dirs))
20+
matlab_root = joinpath(default_dir, maximum(dirs))
2121
end
2222
end
2323
elseif Sys.iswindows()
@@ -26,57 +26,47 @@ function find_matlab_homepath()
2626
dirs = readdir(default_dir)
2727
filter!(dir -> occursin(r"^R[0-9]+[ab]$", dir), dirs)
2828
if !isempty(dirs)
29-
matlab_home = joinpath(default_dir, maximum(dirs))
29+
matlab_root = joinpath(default_dir, maximum(dirs))
3030
end
3131
end
3232
end
3333
end
3434
end
35-
if isnothing(matlab_home)
36-
return nothing
37-
else
38-
@info("Found MATLAB home path at $matlab_home")
39-
return matlab_home
40-
end
35+
!isnothing(matlab_root) && isdir(matlab_root) && @info("Detected MATLAB root folder at \"$matlab_root\"")
36+
return matlab_root
4137
end
4238

43-
function find_matlab_libpath(matlab_home)
39+
function find_matlab_libpath(matlab_root)
4440
# get path to MATLAB libraries
45-
matlab_lib_dir = if Sys.islinux()
41+
matlab_libdir = if Sys.islinux()
4642
Sys.WORD_SIZE == 32 ? "glnx86" : "glnxa64"
4743
elseif Sys.isapple()
4844
Sys.WORD_SIZE == 32 ? "maci" : "maci64"
4945
elseif Sys.iswindows()
5046
Sys.WORD_SIZE == 32 ? "win32" : "win64"
5147
end
52-
matlab_libpath = joinpath(matlab_home, "bin", matlab_lib_dir)
53-
if !isdir(matlab_libpath)
54-
@warn("The MATLAB library path could not be found.")
55-
end
48+
matlab_libpath = joinpath(matlab_root, "bin", matlab_libdir)
49+
isdir(matlab_libpath) && @info("Detected MATLAB library path at \"$matlab_libpath\"")
5650
return matlab_libpath
5751
end
5852

59-
function find_matlab_cmd(matlab_home)
60-
if !Sys.iswindows()
61-
matlab_cmd = joinpath(matlab_home, "bin", "matlab")
62-
if !isfile(matlab_cmd)
63-
@warn("The MATLAB path is invalid. Ensure the \"MATLAB_HOME\" evironmental variable to the MATLAB root directory.")
64-
end
65-
matlab_cmd = "exec $(Base.shell_escape(matlab_cmd))"
66-
elseif Sys.iswindows()
67-
matlab_cmd = joinpath(matlab_home, "bin", (Sys.WORD_SIZE == 32 ? "win32" : "win64"), "MATLAB.exe")
68-
if !isfile(matlab_cmd)
69-
error("The MATLAB path is invalid. Ensure the \"MATLAB_HOME\" evironmental variable to the MATLAB root directory.")
70-
end
53+
function find_matlab_cmd(matlab_root)
54+
if Sys.iswindows()
55+
matlab_cmd = joinpath(matlab_root, "bin", (Sys.WORD_SIZE == 32 ? "win32" : "win64"), "matlab.exe")
56+
isfile(matlab_cmd) && @info("Detected MATLAB executable at \"$matlab_cmd\"")
57+
else
58+
matlab_exe = joinpath(matlab_root, "bin", "matlab")
59+
isfile(matlab_exe) && @info("Detected MATLAB executable at \"$matlab_exe\"")
60+
matlab_cmd = "exec $(Base.shell_escape(matlab_exe))"
7161
end
7262
return matlab_cmd
7363
end
7464

75-
matlab_homepath = find_matlab_homepath()
65+
matlab_root = find_matlab_root()
7666

77-
if !isnothing(matlab_homepath)
78-
matlab_libpath = find_matlab_libpath(matlab_homepath)
79-
matlab_cmd = find_matlab_cmd(matlab_homepath)
67+
if !isnothing(matlab_root)
68+
matlab_libpath = find_matlab_libpath(matlab_root)
69+
matlab_cmd = find_matlab_cmd(matlab_root)
8070
libmx_size = filesize(Libdl.dlpath(joinpath(matlab_libpath, "libmx")))
8171
open(depsfile, "w") do io
8272
println(io,
@@ -115,5 +105,5 @@ elseif get(ENV, "JULIA_REGISTRYCI_AUTOMERGE", nothing) == "true" || get(ENV, "CI
115105
println(io, "const libmx_size = $libmx_size")
116106
end
117107
else
118-
error("MATLAB cannot be found. Set the \"MATLAB_HOME\" environment variable to the MATLAB root directory and re-run Pkg.build(\"MATLAB\").")
108+
error("MATLAB cannot be found. Set the \"MATLAB_ROOT\" environment variable to the MATLAB root directory and re-run Pkg.build(\"MATLAB\").")
119109
end

src/engine.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const default_startflag = "" # no additional flags
99
const default_matlabcmd = matlab_cmd * " -nosplash"
1010
# pass matlab flags directly or as a Vector of flags, i.e. "-a" or ["-a", "-b", "-c"]
1111
startcmd(flag::AbstractString = default_startflag) = isempty(flag) ? default_matlabcmd : default_matlabcmd * " " * flag
12-
startcmd(flags::AbstractVector{T}) where {T<:AbstractString} = isempty(flags) ? default_matlabcmd : default_matlabcmd * " " * join(flags, " ")
12+
startcmd(flags::AbstractVector{<:AbstractString}) = isempty(flags) ? default_matlabcmd : default_matlabcmd * " " * join(flags, " ")
1313

1414
# 64 K buffer should be sufficient to store the output text in most cases
1515
const default_output_buffer_size = 64 * 1024

0 commit comments

Comments
 (0)