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.

VIKTOR's Matlab integration requires a Generic worker.

from viktor.external.generic import GenericAnalysis

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

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

The executable_key in the example above refers to the "matlab" command. This command should also be specified in the configuration file on the server, located in the same directory as the worker:

config.yaml

executables:
matlab:
path: 'C:\path\to\matlab.exe'
arguments:
- '-nodisplay'
- '-nosplash'
- '-nodesktop'
- '-r'
- '\"C:\scripts\runMyModel.m\"'
workingDirectoryPath: 'C:\scripts'
maxParallelProcesses: 1 # must be one, please do not change

Testing

New in v13.5.0

mock_GenericAnalysis decorator for easier testing of GenericAnalysis

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

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

import unittest

from viktor import File
from viktor.testing import mock_GenericAnalysis

from app.my_entity_type.controller import MyEntityTypeController

class TestMyEntityTypeController(unittest.TestCase):

@mock_GenericAnalysis(get_output_file={
'result.xml': File.from_path('test_file.xml'), # <name>: <File>
'result.json': File.from_path('test_file.json'),
...
})
def test_generic_analysis(self):
MyEntityTypeController().generic_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).