OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Share Python object between blocks in Simulink

  • Thread starter Thread starter Arthur
  • Start date Start date
A

Arthur

Guest
I have created a class in Python. In my application this class is a kind of lookup table.

Here is a sample class to illustrate the topic :

Code:
class ExampleClass:

    def __init__(self, var1):
        self.prop1 = var1

    def get_property(self):
        return self.prop1

Matlab offers the ability to call Python modules directly within the Matlab environment (see Matlab docs)

One calls Python like :

Code:
>>> py.importlib.import_module("ExampleClass")
>>> x = py.ExampleClass.ExampleClass(1)
>>> x.get_property()
ans =

     1

What I would like to do is to make x available in Simulink m-function blocks in order to call get_property() from all the m-function blocks. Note that the creation of x is computationally expensive, therefore I want to load it in the workspace once when initializing the simulation.

What I tried so far : I tried passing my ExampleClass to the m-functions via a mask parameter, but I get an error :

Error:Expression 'object name' for type of data 'x' did not evaluate to a valid type.

Which is understandable because Simulink does not allow to use datatypes other than int, double and other standard data types.

Is there a way around to be able to call x.get_property()from my m-functions ?
<p>I have created a class in Python. In my application this class is a kind of lookup table.</p>
<p>Here is a sample class to illustrate the topic :</p>
<pre><code>class ExampleClass:

def __init__(self, var1):
self.prop1 = var1

def get_property(self):
return self.prop1
</code></pre>
<p>Matlab offers the ability to call Python modules directly within the Matlab environment (see <a href="https://fr.mathworks.com/help/matlab/matlab_external/call-user-defined-custom-module.html" rel="nofollow noreferrer">Matlab docs</a>)</p>
<p>One calls Python like :</p>
<pre><code>>>> py.importlib.import_module("ExampleClass")
>>> x = py.ExampleClass.ExampleClass(1)
>>> x.get_property()
ans =

1
</code></pre>
<p>What I would like to do is to make <code>x</code> available in Simulink m-function block<strong>s</strong> in order to call <code>get_property()</code> from all the m-function blocks. Note that the creation of <code>x</code> is computationally expensive, therefore I want to load it in the workspace once when initializing the simulation.</p>
<p>What I tried so far :
I tried passing my <code>ExampleClass</code> to the m-functions via a mask parameter, but I get an error :</p>
<p><code>Error:Expression 'object name' for type of data 'x' did not evaluate to a valid type.</code></p>
<p>Which is understandable because Simulink does not allow to use datatypes other than <code>int</code>, <code>double</code> and other standard data types.</p>
<p>Is there a way around to be able to call <code>x.get_property()</code>from my m-functions ?</p>
 

Latest posts

Online statistics

Members online
0
Guests online
5
Total visitors
5
Top