<?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=Dealing_with_Matlab_files</id>
	<title>Dealing with Matlab files - 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=Dealing_with_Matlab_files"/>
	<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Dealing_with_Matlab_files&amp;action=history"/>
	<updated>2026-10-12T01:54:21Z</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=Dealing_with_Matlab_files&amp;diff=392&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=Dealing_with_Matlab_files&amp;diff=392&amp;oldid=prev"/>
		<updated>2025-10-17T16:59:01Z</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 read and write Matlab files under Python.&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 read and write Matlab files under Python.&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=Dealing_with_Matlab_files&amp;diff=242&amp;oldid=prev</id>
		<title>Davrot: Created page with &quot;== The goal == We want to read and write Matlab files under Python.  Questions to [mailto:davrot@uni-bremen.de David Rotermund]  Reminder: Learning Python as Matlab user Please read [https://numpy.org/doc/stable/user/numpy-for-matlab-users.html NumPy for MATLAB users]  == Mat files under Python ==  === MATLAB &amp;#x3C; 7.3 format mat files === This is a job for [https://docs.scipy.org/doc/scipy/reference/io.html#module-scipy.io scipy.io] {| class=&quot;wikitable&quot; |[https://docs....&quot;</title>
		<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Dealing_with_Matlab_files&amp;diff=242&amp;oldid=prev"/>
		<updated>2025-10-17T13:57:45Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;== The goal == We want to read and write Matlab files under Python.  Questions to [mailto:davrot@uni-bremen.de David Rotermund]  Reminder: Learning Python as Matlab user Please read [https://numpy.org/doc/stable/user/numpy-for-matlab-users.html NumPy for MATLAB users]  == Mat files under Python ==  === MATLAB &amp;lt; 7.3 format mat files === This is a job for [https://docs.scipy.org/doc/scipy/reference/io.html#module-scipy.io scipy.io] {| class=&amp;quot;wikitable&amp;quot; |[https://docs....&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 read and write Matlab files under Python.&lt;br /&gt;
&lt;br /&gt;
Questions to [mailto:davrot@uni-bremen.de David Rotermund]&lt;br /&gt;
&lt;br /&gt;
Reminder: Learning Python as Matlab user Please read [https://numpy.org/doc/stable/user/numpy-for-matlab-users.html NumPy for MATLAB users]&lt;br /&gt;
&lt;br /&gt;
== Mat files under Python ==&lt;br /&gt;
&lt;br /&gt;
=== MATLAB &amp;amp;#x3C; 7.3 format mat files ===&lt;br /&gt;
This is a job for [https://docs.scipy.org/doc/scipy/reference/io.html#module-scipy.io scipy.io]&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|[https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.loadmat.html#scipy.io.loadmat loadmat](file_name[, mdict, appendmat])&lt;br /&gt;
|Load MATLAB file.&lt;br /&gt;
|-&lt;br /&gt;
|[https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.savemat.html#scipy.io.savemat savemat](file_name, mdict[, appendmat, …])&lt;br /&gt;
|Save a dictionary of names and arrays into a MATLAB-style .mat file.&lt;br /&gt;
|-&lt;br /&gt;
|[https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.whosmat.html#scipy.io.whosmat whosmat](file_name[, appendmat])&lt;br /&gt;
|List variables inside a MATLAB file.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==== Read ====&lt;br /&gt;
Under Matlab we create a test file&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;&amp;gt;&amp;gt; A = rand(10,100);&lt;br /&gt;
&amp;gt;&amp;gt; save -v7 Test_1.mat A&amp;lt;/syntaxhighlight&amp;gt;Under Python we look into the file for information:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import scipy.io as sio&lt;br /&gt;
&lt;br /&gt;
Info = sio.whosmat(&amp;quot;Test_1.mat&amp;quot;)&lt;br /&gt;
print(Info) # --&amp;gt; [(&amp;#039;A&amp;#039;, (10, 100), &amp;#039;double&amp;#039;)]&amp;lt;/syntaxhighlight&amp;gt;And we can read the data: &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
import scipy.io as sio&lt;br /&gt;
&lt;br /&gt;
mat_data = sio.loadmat(&amp;quot;Test_1.mat&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
print(&lt;br /&gt;
    mat_data.keys()&lt;br /&gt;
)  # -&amp;gt; dict_keys([&amp;#039;__header__&amp;#039;, &amp;#039;__version__&amp;#039;, &amp;#039;__globals__&amp;#039;, &amp;#039;A&amp;#039;])&lt;br /&gt;
&lt;br /&gt;
a = mat_data[&amp;quot;A&amp;quot;]&lt;br /&gt;
print(type(a))  # --&amp;gt; &amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
print(a.dtype)  # --&amp;gt; float64&lt;br /&gt;
print(a.shape)  # --&amp;gt; (10, 100)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Write ====&lt;br /&gt;
Under Python we generate a .mat file:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
import scipy.io as sio&lt;br /&gt;
&lt;br /&gt;
myrng = np.random.default_rng()&lt;br /&gt;
&lt;br /&gt;
A: np.ndarray = myrng.random((10, 100), dtype=np.float64)&lt;br /&gt;
B: str = &amp;quot;Hellp world!&amp;quot;&lt;br /&gt;
mdic = {&amp;quot;A&amp;quot;: A, &amp;quot;ImportantMessage&amp;quot;: B}&lt;br /&gt;
sio.savemat(&amp;quot;Test_2.mat&amp;quot;, mdic)&amp;lt;/syntaxhighlight&amp;gt;And read it in under Matlab:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;&amp;gt;&amp;gt; load Test_2.mat&lt;br /&gt;
&amp;gt;&amp;gt; whos&lt;br /&gt;
  Name                   Size             Bytes  Class     Attributes&lt;br /&gt;
&lt;br /&gt;
  A                     10x100             8000  double              &lt;br /&gt;
  ImportantMessage       1x12                24  char                &lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; ImportantMessage&lt;br /&gt;
&lt;br /&gt;
ImportantMessage =&lt;br /&gt;
&lt;br /&gt;
    &amp;#039;Hellp world!&amp;#039;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== MATLAB == 7.3 format mat files (for very big files) ===&lt;br /&gt;
&lt;br /&gt;
==== Read ====&lt;br /&gt;
Under Matlab we create another test file&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;&amp;gt;&amp;gt; A = rand(10,100);&lt;br /&gt;
&amp;gt;&amp;gt; B = rand(5, 10,100);&lt;br /&gt;
&amp;gt;&amp;gt; save -v7.3 Test_3.mat A B&amp;lt;/syntaxhighlight&amp;gt;And we can read the data under Python. But be aware that the matrix is in reversed order now. 10x100 -&amp;amp;#x3E; 100x10 and 5x10x100 -&amp;amp;#x3E; 100x10x5&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
import h5py&lt;br /&gt;
&lt;br /&gt;
file_handle = h5py.File(&amp;quot;Test_3.mat&amp;quot;, &amp;quot;r&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
print(file_handle.keys())  # --&amp;gt; &amp;lt;KeysViewHDF5 [&amp;#039;A&amp;#039;]&amp;gt;&lt;br /&gt;
&lt;br /&gt;
hdf5_a = file_handle[&amp;quot;A&amp;quot;]&lt;br /&gt;
print(hdf5_a)  # --&amp;gt; &amp;lt;HDF5 dataset &amp;quot;A&amp;quot;: shape (100, 10), type &amp;quot;&amp;lt;f8&amp;quot;&amp;gt;&lt;br /&gt;
a = np.array(hdf5_a)&lt;br /&gt;
print(type(a))  # --&amp;gt; &amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
print(a.dtype)  # --&amp;gt; float64&lt;br /&gt;
print(a.shape)  # --&amp;gt; (100, 10)&lt;br /&gt;
&lt;br /&gt;
hdf5_b = file_handle[&amp;quot;B&amp;quot;]&lt;br /&gt;
print(hdf5_b)  # --&amp;gt; &amp;lt;HDF5 dataset &amp;quot;B&amp;quot;: shape (100, 10), type &amp;quot;&amp;lt;f8&amp;quot;&amp;gt;&lt;br /&gt;
b = np.array(hdf5_b)&lt;br /&gt;
print(type(b))  # --&amp;gt; &amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
print(b.dtype)  # --&amp;gt; float64&lt;br /&gt;
print(b.shape)  # --&amp;gt; (100, 10, 5)&lt;br /&gt;
&lt;br /&gt;
file_handle.close() #  optional&amp;lt;/syntaxhighlight&amp;gt;Note: If you – in a real world example – see a HDF5 group (e.g. &amp;amp;#x3C;HDF5 group “/#refs#” (16 members)&amp;amp;#x3E; ) instead of a HDF5 dataset then the variable is a container again (very similar to file_handle in the example). It will have keys and you can go down the tree until you find the HDF5 datasets. Especially in the case of Matlab structures you might need to go deeper. &lt;br /&gt;
&lt;br /&gt;
==== Write ====&lt;br /&gt;
Under Python we generate a .hd5 file:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
import h5py&lt;br /&gt;
&lt;br /&gt;
myrng = np.random.default_rng()&lt;br /&gt;
&lt;br /&gt;
a: np.ndarray = myrng.random((10, 100), dtype=np.float64)&lt;br /&gt;
b: np.ndarray = myrng.random((2, 20, 200), dtype=np.float64)&lt;br /&gt;
&lt;br /&gt;
file_handle = h5py.File(&amp;quot;Test_4.hd5&amp;quot;, &amp;quot;w&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
dataset_a = file_handle.create_dataset(&amp;quot;A&amp;quot;, data=a)&lt;br /&gt;
dataset_b = file_handle.create_dataset(&amp;quot;B&amp;quot;, data=b)&lt;br /&gt;
file_handle.close() # optional&amp;lt;/syntaxhighlight&amp;gt;Under Matlab we now can extract information about the hd5 file:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;&amp;gt;&amp;gt; info = h5info(&amp;#039;Test_4.hd5&amp;#039;);&lt;br /&gt;
&amp;gt;&amp;gt; length(info.Datasets)&lt;br /&gt;
&lt;br /&gt;
ans =&lt;br /&gt;
&lt;br /&gt;
     2&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; info.Datasets(1)&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
&lt;br /&gt;
  struct with fields:&lt;br /&gt;
&lt;br /&gt;
          Name: &amp;#039;A&amp;#039;&lt;br /&gt;
      Datatype: [1x1 struct]&lt;br /&gt;
     Dataspace: [1x1 struct]&lt;br /&gt;
     ChunkSize: []&lt;br /&gt;
     FillValue: 0&lt;br /&gt;
       Filters: []&lt;br /&gt;
    Attributes: []&lt;br /&gt;
&lt;br /&gt;
&amp;gt;&amp;gt; info.Datasets(2)&lt;br /&gt;
&lt;br /&gt;
ans = &lt;br /&gt;
&lt;br /&gt;
  struct with fields:&lt;br /&gt;
&lt;br /&gt;
          Name: &amp;#039;B&amp;#039;&lt;br /&gt;
      Datatype: [1x1 struct]&lt;br /&gt;
     Dataspace: [1x1 struct]&lt;br /&gt;
     ChunkSize: []&lt;br /&gt;
     FillValue: 0&lt;br /&gt;
       Filters: []&lt;br /&gt;
    Attributes: []&amp;lt;/syntaxhighlight&amp;gt;And now we read it in under Matlab:&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;&amp;gt;&amp;gt; h5disp(&amp;#039;Test_4.hd5&amp;#039;)&lt;br /&gt;
HDF5 Test_4.hd5 &lt;br /&gt;
Group &amp;#039;/&amp;#039; &lt;br /&gt;
    Dataset &amp;#039;A&amp;#039; &lt;br /&gt;
        Size:  100x10&lt;br /&gt;
        MaxSize:  100x10&lt;br /&gt;
        Datatype:   H5T_IEEE_F64LE (double)&lt;br /&gt;
        ChunkSize:  []&lt;br /&gt;
        Filters:  none&lt;br /&gt;
        FillValue:  0.000000&lt;br /&gt;
    Dataset &amp;#039;B&amp;#039; &lt;br /&gt;
        Size:  200x20x2&lt;br /&gt;
        MaxSize:  200x20x2&lt;br /&gt;
        Datatype:   H5T_IEEE_F64LE (double)&lt;br /&gt;
        ChunkSize:  []&lt;br /&gt;
        Filters:  none&lt;br /&gt;
        FillValue:  0.000000&amp;lt;/syntaxhighlight&amp;gt;Now we know that the databases are at ‘/A’ and ‘/B’. With information we can read the matrices. But be aware that the matrices are in reversed order!&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;&amp;gt;&amp;gt; a = h5read(&amp;#039;Test_4.hd5&amp;#039;,&amp;#039;/A&amp;#039;);&lt;br /&gt;
&amp;gt;&amp;gt; b = h5read(&amp;#039;Test_4.hd5&amp;#039;,&amp;#039;/B&amp;#039;);&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;
  b         200x20x2            64000  double&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Davrot</name></author>
	</entry>
</feed>