<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	>
<channel>
	<title>
	Comments on: Serializing/deserializing Matlab data	</title>
	<atom:link href="https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=serializing-deserializing-matlab-data</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Mon, 23 Sep 2024 19:38:55 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
	<item>
		<title>
		By: Cookie		</title>
		<link>https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data#comment-525412</link>

		<dc:creator><![CDATA[Cookie]]></dc:creator>
		<pubDate>Mon, 23 Sep 2024 19:38:55 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4583#comment-525412</guid>

					<description><![CDATA[If you have the parallel toolbox there is a method &lt;code&gt;byteArray = distcompserialize64(myData)&lt;/code&gt; which appears to function the same as &lt;b&gt;&lt;i&gt;getByteStreamFromArray()&lt;/i&gt;&lt;/b&gt;&#062; but does not seem to have data size limitations.

&lt;em&gt;MATLAB\R2017b\toolbox\distcomp\distcomp\distcompserialize64.mexw64&lt;/em&gt;

There is a function &lt;b&gt;&lt;i&gt;distcompdeserialize()&lt;/i&gt;&lt;/b&gt; which reconstructs the matlab data from the bytes. Fwiw, &lt;b&gt;&lt;i&gt;getArrayFromByteStream()&lt;/i&gt;&lt;/b&gt; appears to work on the serialized bytes produced by &lt;b&gt;&lt;i&gt;distcompserialize64&lt;/i&gt;&lt;/b&gt; as well.  Performance seems to be equivalent for both &lt;b&gt;&lt;i&gt;discompdeserialize()&lt;/i&gt;&lt;/b&gt; and &lt;b&gt;&lt;i&gt;getArrayFromByteStream()&lt;/i&gt;&lt;/b&gt;.]]></description>
			<content:encoded><![CDATA[<p>If you have the parallel toolbox there is a method <code>byteArray = distcompserialize64(myData)</code> which appears to function the same as <b><i>getByteStreamFromArray()</i></b>&gt; but does not seem to have data size limitations.</p>
<p><em>MATLAB\R2017b\toolbox\distcomp\distcomp\distcompserialize64.mexw64</em></p>
<p>There is a function <b><i>distcompdeserialize()</i></b> which reconstructs the matlab data from the bytes. Fwiw, <b><i>getArrayFromByteStream()</i></b> appears to work on the serialized bytes produced by <b><i>distcompserialize64</i></b> as well.  Performance seems to be equivalent for both <b><i>discompdeserialize()</i></b> and <b><i>getArrayFromByteStream()</i></b>.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Blake		</title>
		<link>https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data#comment-435799</link>

		<dc:creator><![CDATA[Blake]]></dc:creator>
		<pubDate>Thu, 16 Aug 2018 13:26:30 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4583#comment-435799</guid>

					<description><![CDATA[This is a great article!  Does anyone know the format of the header information in the converted bytestream?

Thanks!]]></description>
			<content:encoded><![CDATA[<p>This is a great article!  Does anyone know the format of the header information in the converted bytestream?</p>
<p>Thanks!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data#comment-421937</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Mon, 02 Apr 2018 10:15:28 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4583#comment-421937</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data#comment-421936&quot;&gt;Roc Woods&lt;/a&gt;.

@Roc - thanks for the follow-up for the benefit of other readers]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data#comment-421936">Roc Woods</a>.</p>
<p>@Roc &#8211; thanks for the follow-up for the benefit of other readers</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Roc Woods		</title>
		<link>https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data#comment-421936</link>

		<dc:creator><![CDATA[Roc Woods]]></dc:creator>
		<pubDate>Mon, 02 Apr 2018 10:08:08 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4583#comment-421936</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data#comment-421590&quot;&gt;Roc Woods&lt;/a&gt;.

I solved the problem. The problem is that when you compile to DLL, if you use the following command
&lt;pre lang=&quot;matlab&quot;&gt;mcc -W cpplib:testDeserialized -T link:lib testDeserialized.m -C;&lt;/pre&gt; 
the compiler can&#039;t accurately package all the functions that need to be dependent on all the deserialization. At this time, create an empty object that needs to be deserialized, save it, such as nullObj.mat and then pack it with the -a option, and the compiler will analyze the mat file and automatically find the fully dependent functions. As shown below
&lt;pre lang=&quot;matlab&quot;&gt;mcc -W cpplib:testDeserialized -T link:lib testDeserialized.m -a nullObj.mat -C.&lt;/pre&gt; 
Thanks again @Yair]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data#comment-421590">Roc Woods</a>.</p>
<p>I solved the problem. The problem is that when you compile to DLL, if you use the following command</p>
<pre lang="matlab">mcc -W cpplib:testDeserialized -T link:lib testDeserialized.m -C;</pre>
<p>the compiler can&#8217;t accurately package all the functions that need to be dependent on all the deserialization. At this time, create an empty object that needs to be deserialized, save it, such as nullObj.mat and then pack it with the -a option, and the compiler will analyze the mat file and automatically find the fully dependent functions. As shown below</p>
<pre lang="matlab">mcc -W cpplib:testDeserialized -T link:lib testDeserialized.m -a nullObj.mat -C.</pre>
<p>Thanks again @Yair</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Roc Woods		</title>
		<link>https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data#comment-421604</link>

		<dc:creator><![CDATA[Roc Woods]]></dc:creator>
		<pubDate>Wed, 28 Mar 2018 15:46:40 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4583#comment-421604</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data#comment-421590&quot;&gt;Roc Woods&lt;/a&gt;.

@Yair, Thank you very much. I tried your suggestion, but it still has the same result as previous. It is noteworthy that the following code works fine in MATLAB:
&lt;pre lang=&quot;matlab&quot;&gt;
function testDeserialized(matfilename)
   fileData = load(matfilename);
   netByte2 = uint8(fileData.netByteSaved);  % convert back to uint8
   net2 = getArrayFromByteStream(netByte2);
   save D:\net00.mat net2
&lt;/pre&gt;
but when I use the following code
&lt;pre lang=&quot;matlab&quot;&gt;mcc -W cpplib:testDeserialized -T link:lib testDeserialized.m  -C;&lt;/pre&gt;
generate deployed files, such as testDeserialized.dll, testDeserialized.lib, testDeserialized.ctf and call testDeserialized.dll from C++ it has error when I tried to load D:\net00.mat. The error message is as follows:
&lt;pre lang=&quot;matlab&quot;&gt;
load(&#039;D:\net00.mat&#039;)
Error using load
Cannot read file D:\net00.mat. 
try open(&#039;load(&#039;&#039;D:\net00.mat&#039;&#039;)
          ↑
Error: Character vector is not terminated properly.
&lt;/pre&gt;
when double click net00.mat,the error message changed to be
&lt;pre lang=&quot;matlab&quot;&gt;
Warning: Unable to read some of the variables due to unknown MAT-file error.
 
&gt; In matfinfo (line 9)
  In finfo (line 118)
  In internal.matlab.importtool.ImportableFileIdentifier.isTextFile (line 113)
  In internal.matlab.importtool.ImportableFileIdentifier.useTextImportTool (line 91)
  In uiimport&gt;useTextImportTool (line 998)
  In uiimport (line 237) 
Warning: Unable to read some of the variables due to unknown MAT-file error.
 
&gt; In matfinfo (line 9)
  In finfo (line 118)
  In uiimport/gatherFilePreviewData (line 416)
  In uiimport (line 245) 
Error using load
Number of columns on line 2 of ASCII file D:\net00.mat must be the same as previous lines.
Error in uiimport/runImportdata (line 467)
                    datastruct = load(&#039;-ascii&#039;, fileAbsolutePath);
Error in uiimport/gatherFilePreviewData (line 435)
        [datastruct, textDelimiter, headerLines]= runImportdata(fileAbsolutePath,type);
Error in uiimport (line 245)
    gatherFilePreviewData(fileAbsolutePath); 
&lt;/pre&gt;

Do you have any good Suggestions? Thanks.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data#comment-421590">Roc Woods</a>.</p>
<p>@Yair, Thank you very much. I tried your suggestion, but it still has the same result as previous. It is noteworthy that the following code works fine in MATLAB:</p>
<pre lang="matlab">
function testDeserialized(matfilename)
   fileData = load(matfilename);
   netByte2 = uint8(fileData.netByteSaved);  % convert back to uint8
   net2 = getArrayFromByteStream(netByte2);
   save D:\net00.mat net2
</pre>
<p>but when I use the following code</p>
<pre lang="matlab">mcc -W cpplib:testDeserialized -T link:lib testDeserialized.m  -C;</pre>
<p>generate deployed files, such as testDeserialized.dll, testDeserialized.lib, testDeserialized.ctf and call testDeserialized.dll from C++ it has error when I tried to load D:\net00.mat. The error message is as follows:</p>
<pre lang="matlab">
load('D:\net00.mat')
Error using load
Cannot read file D:\net00.mat. 
try open('load(''D:\net00.mat'')
          ↑
Error: Character vector is not terminated properly.
</pre>
<p>when double click net00.mat,the error message changed to be</p>
<pre lang="matlab">
Warning: Unable to read some of the variables due to unknown MAT-file error.
 
> In matfinfo (line 9)
  In finfo (line 118)
  In internal.matlab.importtool.ImportableFileIdentifier.isTextFile (line 113)
  In internal.matlab.importtool.ImportableFileIdentifier.useTextImportTool (line 91)
  In uiimport>useTextImportTool (line 998)
  In uiimport (line 237) 
Warning: Unable to read some of the variables due to unknown MAT-file error.
 
> In matfinfo (line 9)
  In finfo (line 118)
  In uiimport/gatherFilePreviewData (line 416)
  In uiimport (line 245) 
Error using load
Number of columns on line 2 of ASCII file D:\net00.mat must be the same as previous lines.
Error in uiimport/runImportdata (line 467)
                    datastruct = load('-ascii', fileAbsolutePath);
Error in uiimport/gatherFilePreviewData (line 435)
        [datastruct, textDelimiter, headerLines]= runImportdata(fileAbsolutePath,type);
Error in uiimport (line 245)
    gatherFilePreviewData(fileAbsolutePath); 
</pre>
<p>Do you have any good Suggestions? Thanks.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data#comment-421592</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 28 Mar 2018 10:44:07 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4583#comment-421592</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data#comment-421590&quot;&gt;Roc Woods&lt;/a&gt;.

@Roc - try to convert your data to int16 before saving, and then convert back to uint8 after loading:
&lt;pre lang=&quot;matlab&quot;&gt;
netByte = getByteStreamFromArray(net);
netByteSaved = int16(netByte);  % convert to int16
save(filename, &#039;netByteSaved&#039;);
...
fileData = load(filename);
netByte2 = uint8(fileData.netByteSaved);  % convert back to uint8
net2 = getArrayFromByteStream(netByte2);
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data#comment-421590">Roc Woods</a>.</p>
<p>@Roc &#8211; try to convert your data to int16 before saving, and then convert back to uint8 after loading:</p>
<pre lang="matlab">
netByte = getByteStreamFromArray(net);
netByteSaved = int16(netByte);  % convert to int16
save(filename, 'netByteSaved');
...
fileData = load(filename);
netByte2 = uint8(fileData.netByteSaved);  % convert back to uint8
net2 = getArrayFromByteStream(netByte2);
</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Roc Woods		</title>
		<link>https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data#comment-421590</link>

		<dc:creator><![CDATA[Roc Woods]]></dc:creator>
		<pubDate>Wed, 28 Mar 2018 10:10:10 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4583#comment-421590</guid>

					<description><![CDATA[Thanks Yair, this would be very useful. but, there seems to be a bug for some objects to be deserialized with getArrayFromByteStream in deployed mode (dll). Take the AlexNet（we can get the alexnet from Add-On Explorer）for example: when we serialize and deserialized alexnet using the following code:
&lt;pre lang=&quot;matlab&quot;&gt;
net = alexnet;
netByte = getByteStreamFromArray(net);
net2 = getArrayFromByteStream(netByte);
&lt;/pre&gt;
it performs well in both MATLAB or deployed mode.

but if we save netByte in a mat file in advance, then it does not work in dll mode:
&lt;pre lang=&quot;matlab&quot;&gt;
netByte = load(&#039;netByte.mat&#039;);
netByte = netByte.netByte;
net2 = getArrayFromByteStream(netByte);
save D:\net2.mat net2
&lt;/pre&gt;
We can not load the net2.mat. It doesn&#039;t look like to be created correctly.

Do you have any ideas for solving this problem?

Thanks!]]></description>
			<content:encoded><![CDATA[<p>Thanks Yair, this would be very useful. but, there seems to be a bug for some objects to be deserialized with getArrayFromByteStream in deployed mode (dll). Take the AlexNet（we can get the alexnet from Add-On Explorer）for example: when we serialize and deserialized alexnet using the following code:</p>
<pre lang="matlab">
net = alexnet;
netByte = getByteStreamFromArray(net);
net2 = getArrayFromByteStream(netByte);
</pre>
<p>it performs well in both MATLAB or deployed mode.</p>
<p>but if we save netByte in a mat file in advance, then it does not work in dll mode:</p>
<pre lang="matlab">
netByte = load('netByte.mat');
netByte = netByte.netByte;
net2 = getArrayFromByteStream(netByte);
save D:\net2.mat net2
</pre>
<p>We can not load the net2.mat. It doesn&#8217;t look like to be created correctly.</p>
<p>Do you have any ideas for solving this problem?</p>
<p>Thanks!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Serializing MATLAB data &#124; Possibly Wrong		</title>
		<link>https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data#comment-386485</link>

		<dc:creator><![CDATA[Serializing MATLAB data &#124; Possibly Wrong]]></dc:creator>
		<pubDate>Wed, 24 Aug 2016 14:58:50 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4583#comment-386485</guid>

					<description><![CDATA[[&#8230;] actually a very simple and robust built-in solution&#8230; as long as we&#8217;re comfortable with undocumented functionality.  The function b=getByteStreamFromArray(v) converts a value to a uint8 array of [&#8230;]]]></description>
			<content:encoded><![CDATA[<p>[&#8230;] actually a very simple and robust built-in solution&#8230; as long as we&#8217;re comfortable with undocumented functionality.  The function b=getByteStreamFromArray(v) converts a value to a uint8 array of [&#8230;]</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Sam Roberts		</title>
		<link>https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data#comment-370315</link>

		<dc:creator><![CDATA[Sam Roberts]]></dc:creator>
		<pubDate>Mon, 22 Feb 2016 10:11:32 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4583#comment-370315</guid>

					<description><![CDATA[It&#039;s worth noting that &lt;code&gt;getByteStreamFromArray&lt;/code&gt; calls &lt;code&gt;saveobj&lt;/code&gt;. For regular MATLAB variables, this won&#039;t make any difference, but if you have a MATLAB class, you can overload &lt;code&gt;saveobj&lt;/code&gt;, and then &lt;code&gt;getByteStreamFromArray&lt;/code&gt; will be serializing the output of &lt;code&gt;saveobj&lt;/code&gt;. (This is why &lt;code&gt;Transient&lt;/code&gt; and &lt;code&gt;Dependent&lt;/code&gt; properties are not serialized).

Analogously, &lt;code&gt;getArrayFromByteStream&lt;/code&gt; also calls &lt;code&gt;loadobj&lt;/code&gt;.]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s worth noting that <code>getByteStreamFromArray</code> calls <code>saveobj</code>. For regular MATLAB variables, this won&#8217;t make any difference, but if you have a MATLAB class, you can overload <code>saveobj</code>, and then <code>getByteStreamFromArray</code> will be serializing the output of <code>saveobj</code>. (This is why <code>Transient</code> and <code>Dependent</code> properties are not serialized).</p>
<p>Analogously, <code>getArrayFromByteStream</code> also calls <code>loadobj</code>.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Martin		</title>
		<link>https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data#comment-331472</link>

		<dc:creator><![CDATA[Martin]]></dc:creator>
		<pubDate>Fri, 12 Sep 2014 23:50:11 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=4583#comment-331472</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data#comment-311119&quot;&gt;Andre Kuehne&lt;/a&gt;.

@Andre – The limit is either 2 or 4 GB depending on the type of data, since the format uses 32-bit signed integers in some places and 32-bit unsigned integers in other places. If we stick to plain arrays, the limit is 2^32-1 bytes or entries in one dimensions, i.e. this stays within the limit (and thus works) &lt;pre lang=&quot;matlab&quot;&gt;getByteStreamFromArray(zeros(1, 2 ^ 31 - 1, &#039;uint8&#039;));&lt;/pre&gt; while 
&lt;pre lang=&quot;matlab&quot;&gt;getByteStreamFromArray(zeros(1, 2 ^ 31, &#039;uint8&#039;));
getByteStreamFromArray(zeros(0, 2 ^ 31, &#039;uint8&#039;));
&lt;/pre&gt; both fail.

If you use aggregate data types, e.g. cells or structs, then the limit is 4 GB. You can put 3 arrays of size 1 GB into a cell array and then successfully it.

(However, there&#039;s an elegant way to get beyond this limitation. Watch out for one of my future comments.)]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/serializing-deserializing-matlab-data#comment-311119">Andre Kuehne</a>.</p>
<p>@Andre – The limit is either 2 or 4 GB depending on the type of data, since the format uses 32-bit signed integers in some places and 32-bit unsigned integers in other places. If we stick to plain arrays, the limit is 2^32-1 bytes or entries in one dimensions, i.e. this stays within the limit (and thus works) </p>
<pre lang="matlab">getByteStreamFromArray(zeros(1, 2 ^ 31 - 1, 'uint8'));</pre>
<p> while </p>
<pre lang="matlab">getByteStreamFromArray(zeros(1, 2 ^ 31, 'uint8'));
getByteStreamFromArray(zeros(0, 2 ^ 31, 'uint8'));
</pre>
<p> both fail.</p>
<p>If you use aggregate data types, e.g. cells or structs, then the limit is 4 GB. You can put 3 arrays of size 1 GB into a cell array and then successfully it.</p>
<p>(However, there&#8217;s an elegant way to get beyond this limitation. Watch out for one of my future comments.)</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
