Skip to main content

Matlab

Matlab can be executed through command-line instructions, which allows executing complete Matlab scripts. These scripts can, for example, be generated dynamically within VIKTOR. See the Matlab documentation for more info on running Matlab from the command-line.

Add integration to app config

To make the worker integration available through the interface, add the following to your viktor.config.toml:

worker_integrations = [
"matlab",
]

Instructions on how to install the worker:

Install the worker

Follow these steps to install the worker:

Personal workers are installed and managed with VIKTOR Desktop:

  1. Download and install VIKTOR Desktop and log in with your VIKTOR account, if you haven't done so already

  2. In VIKTOR Desktop, click "Add" and select Matlab

  3. Set the path to the Matlab executable as the executable path
  4. Start the worker. You can view its logs inside VIKTOR Desktop, and the editor shows when your worker is online

note

Personal workers that were installed with the previous per-worker installer keep running, but new personal workers can only be added through VIKTOR Desktop: the per-worker installer download and connection-key flow are no longer available for personal use.

App code

import viktor as vkt

# Generate the input file(s)
files = [
('input1.txt', file1),
('input2.txt', file2)
]

# Run the analysis and obtain the output file
analysis = vkt.matlab.MatlabAnalysis(files=files, output_filenames=["output.txt"])
analysis.execute(timeout=60)
output_file = analysis.get_output_file("output.txt")

Testing

MatlabAnalysis.execute needs to be mocked within the context of (automated) testing.

The viktor.testing module provides the mock_MatlabAnalysis decorator that facilitate mocking of workers:

import unittest

import viktor as vkt

from viktor.testing import mock_MatlabAnalysis

from app.my_entity_type.controller import MyEntityTypeController


class TestMyEntityTypeController(unittest.TestCase):
@mock_MatlabAnalysis(get_output_file={
'result.xml': vkt.File.from_path('test_file.xml'), # <name>: <File>
'result.json': vkt.File.from_path('test_file.json'),
...
})
def test_analysis(self):
MyEntityTypeController().analysis()

For the decorator's input parameters the following holds:

  • If a Sequence type is provided, the next entry is returned for each corresponding method call. When a call is performed on a depleted iterable, an Exception is raised.
  • If a single object is provided, the object is returned each time the corresponding method is called (endlessly).
  • If None is provided (default), a default File/BytesIO object (with empty content) is returned each time the corresponding method is called (endlessly).