From 56e6362c79dfd99d95e4b61cb002f886eb6c94c8 Mon Sep 17 00:00:00 2001 From: Stephen Nneji Date: Wed, 2 Jul 2025 15:08:39 +0100 Subject: [PATCH 1/2] changes --- ratapi/matlab.txt | 1 - ratapi/wrappers.py | 31 ++++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 8 deletions(-) delete mode 100644 ratapi/matlab.txt diff --git a/ratapi/matlab.txt b/ratapi/matlab.txt deleted file mode 100644 index 710371e7..00000000 --- a/ratapi/matlab.txt +++ /dev/null @@ -1 +0,0 @@ -C:\Program Files\MATLAB\R2024b\bin\matlab.EXE \ No newline at end of file diff --git a/ratapi/wrappers.py b/ratapi/wrappers.py index 6c3d55db..2375f797 100644 --- a/ratapi/wrappers.py +++ b/ratapi/wrappers.py @@ -1,5 +1,6 @@ -"""Wrappers for the interface between ratapi and MATLAB custom files.""" +"""Wrappers for the interface between RATapi and MATLAB custom files.""" +import os import pathlib from contextlib import suppress from typing import Callable @@ -7,7 +8,7 @@ import numpy as np from numpy.typing import ArrayLike -import ratapi.rat_core +import RATapi.rat_core def start_matlab(): @@ -20,10 +21,10 @@ def start_matlab(): """ future = None - with suppress(ImportError): - import matlab.engine - - future = matlab.engine.start_matlab(background=True) + if os.environ.get("DELAY_MATLAB_START", '0') == '0': + with suppress(ImportError): + import matlab.engine + future = matlab.engine.start_matlab(background=True) return future @@ -86,6 +87,22 @@ def handle(*args): return handle +def use_shared_matlab(name, custom_error=""): + """Start MATLAB asynchronously and returns a future to retrieve the engine later. + + Returns + ------- + future : matlab.engine.futureresult.FutureResult + A future used to get the actual matlab engine. + + """ + with suppress(ImportError): + import matlab.engine + + MatlabWrapper.loader = matlab.engine.connect_matlab(name, background=True) + return MatlabWrapper.loader + + class DylibWrapper: """Creates a python callback for a function in dynamic library. @@ -99,7 +116,7 @@ class DylibWrapper: """ def __init__(self, filename, function_name) -> None: - self.engine = ratapi.rat_core.DylibEngine(filename, function_name) + self.engine = RATapi.rat_core.DylibEngine(filename, function_name) def getHandle(self) -> Callable[[ArrayLike, ArrayLike, ArrayLike, int, int], tuple[ArrayLike, float]]: """Return a wrapper for the custom dynamic library function. From 1b14089af33efffdcfe482af0ec0276f41c8ebf6 Mon Sep 17 00:00:00 2001 From: Stephen Nneji Date: Wed, 2 Jul 2025 15:46:01 +0100 Subject: [PATCH 2/2] fix --- ratapi/wrappers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ratapi/wrappers.py b/ratapi/wrappers.py index 2375f797..925a632e 100644 --- a/ratapi/wrappers.py +++ b/ratapi/wrappers.py @@ -1,4 +1,4 @@ -"""Wrappers for the interface between RATapi and MATLAB custom files.""" +"""Wrappers for the interface between ratapi and MATLAB custom files.""" import os import pathlib @@ -8,7 +8,7 @@ import numpy as np from numpy.typing import ArrayLike -import RATapi.rat_core +import ratapi.rat_core def start_matlab():