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.

Install a Matlab worker

Follow these steps to install the worker:

  1. Navigate to the "My Integrations" tab in your personal settings

  2. Click "Add integration"

  3. Follow the steps provided in the modal

    3.1. Select Matlab

    3.2. Download the worker .msi (Microsoft Installer) and run it on the machine of choice

    3.3. Copy the generated connection key and paste it when the installer asks for it. In the browser, you can now click Finish. Continue the installation in the installer wizard.

    Connection Key

    The generated connection key should be copied immediately as VIKTOR will not preserve this data for security reasons.

  4. In the installer wizard, select the Matlab executable

  5. Make sure to launch the worker once the installation is finished. If you closed the integration, you can restart it through the desktop shortcut.

Setup app

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).