Skip to content

Commit de0123b

Browse files
committed
refactor matlab.py
1 parent 3a4b922 commit de0123b

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

ratapi/utils/matlab.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Runs RAT from the MATLAB API."""
22

3-
import os
43
import tempfile
54
import warnings
65
from pathlib import Path
@@ -10,8 +9,17 @@
109
from ..wrappers import MatlabWrapper
1110

1211
RUNNER = """function executeRAT()
12+
13+
cur_dir = pwd;
14+
cd('{rat_path}');
15+
addPaths;
16+
cd(cur_dir);
17+
1318
project = jsonToProject('{project}');
1419
controls = jsonToControls('{control}');
20+
for i=1:project.customFile.rowCount
21+
addpath(project.customFile.varTable{{i, 5}});
22+
end
1523
[project, results] = RAT(project, controls);
1624
1725
projectToJson(project, '{project}');
@@ -36,7 +44,6 @@ def run_matlab_directly(project, controls, matlab_rat_path):
3644
raise ImportError(MatlabWrapper.loader_error_message) from None
3745

3846
engine = MatlabWrapper.loader.result()
39-
cur_dir = os.getcwd()
4047

4148
with tempfile.TemporaryDirectory() as tmp:
4249
project_file = Path(tmp, "project.json")
@@ -45,22 +52,18 @@ def run_matlab_directly(project, controls, matlab_rat_path):
4552
runner_file = Path(tmp, "executeRAT.m")
4653

4754
with open(runner_file, "w") as f:
48-
f.write(RUNNER.format(project=project_file, control=control_file, result=result_file))
55+
f.write(
56+
RUNNER.format(project=project_file, control=control_file, result=result_file, rat_path=matlab_rat_path)
57+
)
4958

5059
with warnings.catch_warnings(): # Avoid warning about relative paths
5160
warnings.simplefilter("ignore")
5261
project.save(project_file)
5362
controls.save(control_file)
5463

55-
engine.cd(matlab_rat_path, nargout=0)
56-
engine.eval("addPaths", nargout=0)
57-
engine.cd(cur_dir, nargout=0)
58-
5964
engine.addpath(tmp, nargout=0)
60-
for file in project.custom_files:
61-
engine.addpath(str(file.path), nargout=0)
62-
6365
engine.executeRAT(nargout=0)
66+
engine.rmpath(tmp, nargout=0)
6467

6568
project = Project.load(project_file)
6669
results = Results.load(result_file)

0 commit comments

Comments
 (0)