Skip to content

Commit 838e852

Browse files
committedFeb 21, 2025
add warnings for large arrays
add warnings for large arrays
1 parent ddf3968 commit 838e852

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed
 

‎src/engine.jl

+6-1
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,13 @@ function put_variable(session::MSession, name::Symbol, v::MxArray)
180180
string(name),
181181
v,
182182
)
183+
184+
# The size limit for `put_variable` is 2GB according to the MATLAB documentation, but seems to be a bit higher in practice.
185+
# https://de.mathworks.com/help/matlab/apiref/engputvariable.html
183186
ret != 0 && throw(
184-
MEngineError("failed to put variable $(name) into MATLAB session (err = $ret)"),
187+
MEngineError(
188+
"failed to put variable $(name) into MATLAB session (err = $ret). Ensure the that the variable name does not conflict with internal MATLAB names and that the size of the variable is below the MATLAB limit of 2GB.",
189+
),
185190
)
186191
return nothing
187192
end

‎src/mxarray.jl

+16
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,14 @@ mxarray(x::T) where {T<:MxComplexNum} = mxarray([x])
256256
# mxArray use Julia array's memory
257257

258258
function mxarray(a::Array{T}) where {T<:MxRealNum}
259+
# Check the array size
260+
if sizeof(a) > 2 * 1024^3
261+
@warn(
262+
"Input array size exceeds the limit of 2 GB and is too large to be used with the MATLAB engine.",
263+
maxlog = 1
264+
)
265+
end
266+
259267
mx = mxarray(T, size(a))
260268
ccall(
261269
:memcpy,
@@ -269,6 +277,14 @@ function mxarray(a::Array{T}) where {T<:MxRealNum}
269277
end
270278

271279
function mxarray(a::Array{T}) where {T<:MxComplexNum}
280+
# Check the array size
281+
if sizeof(a) > 2 * 1024^3
282+
@warn(
283+
"Input array size exceeds the limit of 2 GB and is too large to be used with the MATLAB engine.",
284+
maxlog = 1
285+
)
286+
end
287+
272288
mx = mxarray(T, size(a))
273289
na = length(a)
274290
rdat = unsafe_wrap(Array, real_ptr(mx), na)

0 commit comments

Comments
 (0)