<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://mscneuro.neuro.uni-bremen.de/index.php?action=history&amp;feed=atom&amp;title=Running_Python_remotely_from_Matlab</id>
	<title>Running Python remotely from Matlab - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://mscneuro.neuro.uni-bremen.de/index.php?action=history&amp;feed=atom&amp;title=Running_Python_remotely_from_Matlab"/>
	<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Running_Python_remotely_from_Matlab&amp;action=history"/>
	<updated>2026-06-02T08:20:55Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://mscneuro.neuro.uni-bremen.de/index.php?title=Running_Python_remotely_from_Matlab&amp;diff=394&amp;oldid=prev</id>
		<title>Davrot at 16:59, 17 October 2025</title>
		<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Running_Python_remotely_from_Matlab&amp;diff=394&amp;oldid=prev"/>
		<updated>2025-10-17T16:59:47Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 16:59, 17 October 2025&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;== The goal ==&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We want to integrate Python files and code into our Matlab workflow.&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We want to integrate Python files and code into our Matlab workflow.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Davrot</name></author>
	</entry>
	<entry>
		<id>https://mscneuro.neuro.uni-bremen.de/index.php?title=Running_Python_remotely_from_Matlab&amp;diff=245&amp;oldid=prev</id>
		<title>Davrot: Created page with &quot;== The goal == We want to integrate Python files and code into our Matlab workflow.  Questions to [mailto:davrot@uni-bremen.de David Rotermund]  == Check if you are ready == [https://de.mathworks.com/support/requirements/python-compatibility.html Versions of Python Compatible with MATLAB Products by Release]  You need to ask yourself or better Matlab if it is using the correct Python. You can check this via:&lt;syntaxhighlight lang=&quot;matlab&quot;&gt;&gt;&gt; pyenv  ans =     PythonEnviron...&quot;</title>
		<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Running_Python_remotely_from_Matlab&amp;diff=245&amp;oldid=prev"/>
		<updated>2025-10-17T14:05:05Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;== The goal == We want to integrate Python files and code into our Matlab workflow.  Questions to [mailto:davrot@uni-bremen.de David Rotermund]  == Check if you are ready == [https://de.mathworks.com/support/requirements/python-compatibility.html Versions of Python Compatible with MATLAB Products by Release]  You need to ask yourself or better Matlab if it is using the correct Python. You can check this via:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;&amp;gt;&amp;gt; pyenv  ans =     PythonEnviron...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== The goal ==&lt;br /&gt;
We want to integrate Python files and code into our Matlab workflow.&lt;br /&gt;
&lt;br /&gt;
Questions to [mailto:davrot@uni-bremen.de David Rotermund]&lt;br /&gt;
&lt;br /&gt;
== Check if you are ready ==&lt;br /&gt;
[https://de.mathworks.com/support/requirements/python-compatibility.html Versions of Python Compatible with MATLAB Products by Release]&lt;br /&gt;
&lt;br /&gt;
You need to ask yourself or better Matlab if it is using the correct Python. You can check this via:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;&amp;gt;&amp;gt; pyenv&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
&lt;br /&gt;
  PythonEnvironment with properties:&lt;br /&gt;
&lt;br /&gt;
          Version: &amp;quot;3.11&amp;quot;&lt;br /&gt;
       Executable: &amp;quot;/data_1/davrot/P3.11/bin/python3&amp;quot;&lt;br /&gt;
          Library: &amp;quot;libpython3.11.so.1.0&amp;quot;&lt;br /&gt;
             Home: &amp;quot;/data_1/davrot/P3.11&amp;quot;&lt;br /&gt;
           Status: NotLoaded&lt;br /&gt;
    ExecutionMode: InProcess&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;If this is wrong (which it isn’t in my case) then you can change it (temporarily?) with&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;&amp;gt;&amp;gt; pyenv(&amp;#039;Version&amp;#039;,&amp;#039;/data_1/davrot/P3.11/bin/python3&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
&lt;br /&gt;
  PythonEnvironment with properties:&lt;br /&gt;
&lt;br /&gt;
          Version: &amp;quot;3.11&amp;quot;&lt;br /&gt;
       Executable: &amp;quot;/data_1/davrot/P3.11/bin/python3&amp;quot;&lt;br /&gt;
          Library: &amp;quot;libpython3.11.so.1.0&amp;quot;&lt;br /&gt;
             Home: &amp;quot;/data_1/davrot/P3.11&amp;quot;&lt;br /&gt;
           Status: NotLoaded&lt;br /&gt;
    ExecutionMode: InProcess&amp;lt;/syntaxhighlight&amp;gt;&amp;#039;&amp;#039;&amp;#039;Obviously you need to use your location for your Python installation.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Python help ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;&amp;gt;&amp;gt; py.help(&amp;#039;int&amp;#039;)&lt;br /&gt;
Help on class int in module builtins:&lt;br /&gt;
&lt;br /&gt;
class int(object)&lt;br /&gt;
 |  int([x]) -&amp;gt; integer&lt;br /&gt;
 |  int(x, base=10) -&amp;gt; integer&lt;br /&gt;
[...]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Tuple and random number example ==&lt;br /&gt;
We can crate a python tuple like this:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;&amp;gt;&amp;gt; py_dim = py.tuple({py.int(10), py.int(100)})&lt;br /&gt;
&lt;br /&gt;
py_dim = &lt;br /&gt;
&lt;br /&gt;
  Python tuple with values:&lt;br /&gt;
&lt;br /&gt;
    (10, 100)&lt;br /&gt;
&lt;br /&gt;
    Use string, double or cell function to convert to a MATLAB array.&amp;lt;/syntaxhighlight&amp;gt;Now we can use numpy to generate random numbers:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;&amp;gt;&amp;gt; py_dim = py.tuple({py.int(10), py.int(100)})&lt;br /&gt;
&amp;gt;&amp;gt; rng = py.numpy.random.default_rng();&lt;br /&gt;
&amp;gt;&amp;gt; a = rng.random(py_dim);&lt;br /&gt;
&amp;gt;&amp;gt; py.print(py.type(a))&lt;br /&gt;
&amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt; py.print(a.shape)&lt;br /&gt;
(10, 100)&lt;br /&gt;
&amp;gt;&amp;gt; py.print(a.dtype)&lt;br /&gt;
float64&lt;br /&gt;
&amp;gt;&amp;gt; whos&lt;br /&gt;
&amp;gt;&amp;gt; whos&lt;br /&gt;
  Name        Size            Bytes  Class                                   Attributes&lt;br /&gt;
&lt;br /&gt;
  a           1x1                 8  py.numpy.ndarray                                  &lt;br /&gt;
  ans         1x1                 8  matlab.pyclient.PythonEnvironment                 &lt;br /&gt;
  py_dim      1x2                 8  py.tuple                                          &lt;br /&gt;
  rng         1x1                 8  py.numpy.random._generator.Generator    &amp;lt;/syntaxhighlight&amp;gt;Alternatively this is also possible:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;&amp;gt;&amp;gt; b = rng.random(cell({int32(10),int32(100)}));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== User defined Python modules ==&lt;br /&gt;
Our very own Python function in the file &amp;#039;&amp;#039;&amp;#039;mtest_1.py&amp;#039;&amp;#039;&amp;#039;:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
&lt;br /&gt;
def mysquared(input:np.ndarray) -&amp;gt; np.ndarray:&lt;br /&gt;
    output = input**2&lt;br /&gt;
    return output&amp;lt;/syntaxhighlight&amp;gt;Now Matlab allows us to do this:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;&amp;gt;&amp;gt; x = (1:1:10);&lt;br /&gt;
&amp;gt;&amp;gt; x_np = py.numpy.array(x);&lt;br /&gt;
&amp;gt;&amp;gt; y_np = py.mtest_1.mysquared(x_np);&lt;br /&gt;
&amp;gt;&amp;gt; y = double(x_np).^2;&lt;br /&gt;
&amp;gt;&amp;gt; sum(sum(abs(y-double(y_np))))&lt;br /&gt;
&lt;br /&gt;
ans =&lt;br /&gt;
&lt;br /&gt;
     0&amp;lt;/syntaxhighlight&amp;gt;If you change the py file, then you need to clean it from the memory via&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;&amp;gt;&amp;gt; clear classes&amp;lt;/syntaxhighlight&amp;gt;Future David here: Well, I had to do this for a file gauss_smear.py with a function gauss_smear:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;if ~exist(&amp;#039;mod&amp;#039;, &amp;#039;var&amp;#039;)&lt;br /&gt;
    mod = py.importlib.import_module(&amp;#039;gauss_smear&amp;#039;);&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
py.importlib.reload(mod);&lt;br /&gt;
&lt;br /&gt;
clearvars -except mod&lt;br /&gt;
mod.gauss_smear(2.0, 0.1)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Save a numpy file with Matlab ==&lt;br /&gt;
In Matlab we save data into numpy file:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;&amp;gt;&amp;gt; a = rand(100,10);&lt;br /&gt;
&amp;gt;&amp;gt; a_np = py.numpy.array(a);&lt;br /&gt;
&amp;gt;&amp;gt; py.numpy.save(&amp;quot;test_1.npy&amp;quot;,a_np);&amp;lt;/syntaxhighlight&amp;gt;Now we can load it into Python:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
&lt;br /&gt;
a = np.load(&amp;quot;test_1.npy&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
print(type(a)) # --&amp;gt; &amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
print(a.shape) # --&amp;gt; (100, 10)&lt;br /&gt;
print(a.dtype) # --&amp;gt; float64&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Loading a numpy file with Matlab ==&lt;br /&gt;
Under Python we generate a file:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
&lt;br /&gt;
myrng = np.random.default_rng()&lt;br /&gt;
&lt;br /&gt;
a = myrng.random((100, 10))&lt;br /&gt;
np.save(&amp;quot;test_2.npy&amp;quot;, a)&amp;lt;/syntaxhighlight&amp;gt;And under Matlab we load it:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;&amp;gt;&amp;gt; a_np = py.numpy.load(&amp;quot;test_2.npy&amp;quot;);&lt;br /&gt;
&amp;gt;&amp;gt; a = double(a_np);&lt;br /&gt;
&amp;gt;&amp;gt; whos&lt;br /&gt;
  Name        Size            Bytes  Class               Attributes&lt;br /&gt;
&lt;br /&gt;
  a         100x10             8000  double                        &lt;br /&gt;
  a_np        1x1                 8  py.numpy.ndarray     &amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
* [https://de.mathworks.com/help/matlab/matlab_external/create-object-from-python-class.html Access Python Modules from MATLAB - Getting Started]&lt;br /&gt;
* [https://de.mathworks.com/help/matlab/matlab_external/pythonstrvariables.html Use Python str Variables in MATLAB]&lt;br /&gt;
* [https://de.mathworks.com/help/matlab/matlab_external/pythonlistvariables.html Use Python list Variables in MATLAB]&lt;br /&gt;
* [https://de.mathworks.com/help/matlab/matlab_external/pythontuplevariables.html Use Python tuple Variables in MATLAB]&lt;br /&gt;
* [https://de.mathworks.com/help/matlab/matlab_external/python-dict-variables.html Use Python dict Variables in MATLAB]&lt;br /&gt;
* [https://de.mathworks.com/help/matlab/matlab_external/call-user-defined-custom-module.html Call User-Defined Python Module]&lt;/div&gt;</summary>
		<author><name>Davrot</name></author>
	</entry>
</feed>