Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
tqml committed Feb 20, 2025
1 parent 5721d75 commit ec2fb7d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/engine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ 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.", maxlog = 1)
if Sys.iswindows()
@warn(
"Ensure `matlab -regserver` has been run in a Command Prompt as Administrator.",
maxlog=1
maxlog = 1
)
elseif Sys.islinux()
@warn(
"Ensure `csh` is installed; this may require running `sudo apt-get install csh`.",
maxlog=1
maxlog = 1
)
end
throw(MEngineError("failed to open MATLAB engine session"))
Expand Down
46 changes: 30 additions & 16 deletions src/mxarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ function mxarray(a::Array{T}) where {T<:MxRealNum}
(Ptr{Cvoid}, Ptr{Cvoid}, UInt),
data_ptr(mx),
a,
length(a)*sizeof(T),
length(a) * sizeof(T),
)
return mx
end
Expand Down Expand Up @@ -374,7 +374,7 @@ function _copy_sparse_mat(
ir[i] = rinds[i] - 1
end

jc = unsafe_wrap(Array, jc_p, (n+1,))
jc = unsafe_wrap(Array, jc_p, (n + 1,))
for i = 1:(n+1)
jc[i] = colptr[i] - 1
end
Expand Down Expand Up @@ -402,7 +402,7 @@ function _copy_sparse_mat(
ir[i] = rinds[i] - 1
end

jc = unsafe_wrap(Array, jc_p, (n+1,))
jc = unsafe_wrap(Array, jc_p, (n + 1,))
for i = 1:(n+1)
jc[i] = colptr[i] - 1
end
Expand All @@ -414,7 +414,7 @@ function mxarray(a::SparseMatrixCSC{V,I}) where {V<:Union{Float64,ComplexF64,Boo
m::Int = a.m
n::Int = a.n
nnz = length(a.nzval)
@assert nnz == a.colptr[n+1]-1
@assert nnz == a.colptr[n+1] - 1

mx = mxsparse(V, m, n, nnz)
ir_p = ccall(mx_get_ir[], Ptr{mwIndex}, (Ptr{Cvoid},), mx)
Expand All @@ -439,7 +439,7 @@ function mxarray(s::String)
_dims_to_mwSize((1, length(utf16string))))
mx = MxArray(pm)
ccall(:memcpy, Ptr{Cvoid}, (Ptr{Cvoid}, Ptr{Cvoid}, UInt), data_ptr(mx), utf16string,
length(utf16string)*sizeof(UInt16))
length(utf16string) * sizeof(UInt16))
return mx
end

Expand All @@ -453,7 +453,7 @@ end
mxcellarray(dims::Integer...) = mxcellarray(dims)

function get_cell(mx::MxArray, i::Integer)
pm = ccall(mx_get_cell[], Ptr{Cvoid}, (Ptr{Cvoid}, mwIndex), mx, i-1)
pm = ccall(mx_get_cell[], Ptr{Cvoid}, (Ptr{Cvoid}, mwIndex), mx, i - 1)
MxArray(pm, false)
end

Expand Down Expand Up @@ -505,7 +505,7 @@ function set_field(mx::MxArray, i::Integer, f::String, v::MxArray)
Cvoid,
(Ptr{Cvoid}, mwIndex, Ptr{UInt8}, Ptr{Cvoid}),
mx,
i-1,
i - 1,
f,
v,
)
Expand All @@ -515,23 +515,30 @@ end
set_field(mx::MxArray, f::String, v::MxArray) = set_field(mx, 1, f, v)

function get_field(mx::MxArray, i::Integer, f::String)
pm = ccall(mx_get_field[], Ptr{Cvoid}, (Ptr{Cvoid}, mwIndex, Ptr{UInt8}), mx, i-1, f)
pm = ccall(mx_get_field[], Ptr{Cvoid}, (Ptr{Cvoid}, mwIndex, Ptr{UInt8}), mx, i - 1, f)
pm == C_NULL && throw(ArgumentError("Failed to get field."))
MxArray(pm, false)
end

get_field(mx::MxArray, f::String) = get_field(mx, 1, f)

function get_field(mx::MxArray, i::Integer, fn::Integer)
pm = ccall(mx_get_field_bynum[], Ptr{Cvoid}, (Ptr{Cvoid}, mwIndex, Cint), mx, i-1, fn-1)
pm = ccall(
mx_get_field_bynum[],
Ptr{Cvoid},
(Ptr{Cvoid}, mwIndex, Cint),
mx,
i - 1,
fn - 1,
)
pm == C_NULL && throw(ArgumentError("Failed to get field."))
MxArray(pm, false)
end

get_field(mx::MxArray, fn::Integer) = get_field(mx, 1, fn)

function get_fieldname(mx::MxArray, i::Integer)
p = ccall(mx_get_fieldname[], Ptr{UInt8}, (Ptr{Cvoid}, Cint), mx, i-1)
p = ccall(mx_get_fieldname[], Ptr{UInt8}, (Ptr{Cvoid}, Cint), mx, i - 1)
unsafe_string(p)
end

Expand Down Expand Up @@ -606,7 +613,7 @@ function _jarrayx(fun::String, mx::MxArray, siz::Tuple)
(Ptr{Cvoid}, Ptr{Cvoid}, UInt),
a,
data_ptr(mx),
length(a)*sizeof(T),
length(a) * sizeof(T),
)
end
end
Expand Down Expand Up @@ -644,7 +651,7 @@ function jscalar(mx::MxArray)
@assert !is_sparse(mx)
if is_complex(mx)
return unsafe_wrap(Array, real_ptr(mx), (1,))[1] +
im*unsafe_wrap(Array, imag_ptr(mx), (1,))[1]
im * unsafe_wrap(Array, imag_ptr(mx), (1,))[1]
else
return unsafe_wrap(Array, data_ptr(mx), (1,))[1]
end
Expand All @@ -656,18 +663,18 @@ function _jsparse(ty::Type{T}, mx::MxArray) where {T<:MxRealNum}
ir_ptr = ccall(mx_get_ir[], Ptr{mwIndex}, (Ptr{Cvoid},), mx)
jc_ptr = ccall(mx_get_jc[], Ptr{mwIndex}, (Ptr{Cvoid},), mx)

jc_a::Vector{mwIndex} = unsafe_wrap(Array, jc_ptr, (n+1,))
jc_a::Vector{mwIndex} = unsafe_wrap(Array, jc_ptr, (n + 1,))
nnz = jc_a[n+1]

ir = Vector{Int}(undef, nnz)
jc = Vector{Int}(undef, n+1)
jc = Vector{Int}(undef, n + 1)

ir_x = unsafe_wrap(Array, ir_ptr, (nnz,))
for i = 1:nnz
ir[i] = ir_x[i] + 1
end

jc_x = unsafe_wrap(Array, jc_ptr, (n+1,))
jc_x = unsafe_wrap(Array, jc_ptr, (n + 1,))
for i = 1:(n+1)
jc[i] = jc_x[i] + 1
end
Expand Down Expand Up @@ -713,7 +720,14 @@ function Dict(mx::MxArray)
for i = 1:nf
fnames[i] = get_fieldname(mx, i)
pv =
ccall(mx_get_field_bynum[], Ptr{Cvoid}, (Ptr{Cvoid}, mwIndex, Cint), mx, 0, i-1)
ccall(
mx_get_field_bynum[],
Ptr{Cvoid},
(Ptr{Cvoid}, mwIndex, Cint),
mx,
0,
i - 1,
)
fx = MxArray(pv, false)
fvals[i] = jvalue(fx)
end
Expand Down
2 changes: 1 addition & 1 deletion test/matfile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ a32 = Int32[1 2 3; 4 5 6]
a64 = Int64[1 2 3; 4 5 6]
b = [1.2, 3.4, 5.6, 7.8]
c = [[0.0, 1.0], [1.0, 2.0], [1.0, 2.0, 3.0]]
d = Dict("name"=>"MATLAB", "score"=>100.0)
d = Dict("name" => "MATLAB", "score" => 100.0)

struct S
x::Float64
Expand Down
6 changes: 3 additions & 3 deletions test/mxarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ a2 = jvector(a_mx)
@test isequal([1:5;], a2)
delete(a_mx)

a = rand(5, 6) + rand(5, 6)*im
a = rand(5, 6) + rand(5, 6) * im
a_mx = mxarray(a)
a2 = jarray(a_mx)
@test isequal(a, a2)
Expand Down Expand Up @@ -290,7 +290,7 @@ a = mxstruct("abc", "efg", "xyz")
@test get_fieldname(a, 3) == "xyz"
delete(a)

s = Dict("name"=>"MATLAB", "version"=>12.0, "data"=>[1, 2, 3])
s = Dict("name" => "MATLAB", "version" => 12.0, "data" => [1, 2, 3])
a = mxstruct(s)
@test is_struct(a)
@test mxnfields(a) == 3
Expand Down Expand Up @@ -448,7 +448,7 @@ delete(x)
@test y[2] == a[2]
@test y[3] == a[3]

a = Dict("abc"=>10.0, "efg"=>[1, 2, 3], "xyz"=>"MATLAB")
a = Dict("abc" => 10.0, "efg" => [1, 2, 3], "xyz" => "MATLAB")
x = mxarray(a)
y = jvalue(x)
delete(x)
Expand Down

0 comments on commit ec2fb7d

Please sign in to comment.