<?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: String/char compatibility	</title>
	<atom:link href="https://undocumentedmatlab.com/articles/string-char-compatibility/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com/articles/string-char-compatibility?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=string-char-compatibility</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Mon, 08 Feb 2021 18:41:30 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
	<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/string-char-compatibility#comment-509924</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Mon, 08 Feb 2021 18:41:30 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7710#comment-509924</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/string-char-compatibility#comment-509923&quot;&gt;Alex Churchill&lt;/a&gt;.

@Alex - thanks, but keep in mind that new functions will only work on the recent Matlab releases. If your code needs to work on older Matlab releases, you could revert to the previous &lt;i&gt;&quot;hold-your-nose&quot;&lt;/i&gt; undocumented functionality. This can easily be achieved using a simple try-catch block:
&lt;pre lang=&quot;Matlab&quot;&gt;
try
    % use documented functions that only run on very recent Matlab releases
    ...
catch
    % use undocumented functions that work on older Matlab releases
    ...
end
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/string-char-compatibility#comment-509923">Alex Churchill</a>.</p>
<p>@Alex &#8211; thanks, but keep in mind that new functions will only work on the recent Matlab releases. If your code needs to work on older Matlab releases, you could revert to the previous <i>&#8220;hold-your-nose&#8221;</i> undocumented functionality. This can easily be achieved using a simple try-catch block:</p>
<pre lang="Matlab">
try
    % use documented functions that only run on very recent Matlab releases
    ...
catch
    % use undocumented functions that work on older Matlab releases
    ...
end
</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Alex Churchill		</title>
		<link>https://undocumentedmatlab.com/articles/string-char-compatibility#comment-509923</link>

		<dc:creator><![CDATA[Alex Churchill]]></dc:creator>
		<pubDate>Mon, 08 Feb 2021 18:14:04 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7710#comment-509923</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/string-char-compatibility#comment-431277&quot;&gt;Yair Altman&lt;/a&gt;.

I was looking up how to do this exact task today. I was about to hold my nose and use the internal controllib function, when I happened to chance across a slightly newer MathWorks function, convertContainedStringsToChars: &lt;a href=&quot;https://www.mathworks.com/help/matlab/ref/convertcontainedstringstochars.html&quot; target=&quot;_blank&quot; rel=&quot;nofollow&quot;&gt;https://www.mathworks.com/help/matlab/ref/convertcontainedstringstochars.html&lt;/a&gt; 

That function recurses in just the way I want it to, and it meets several of your other benefits: it works recursively inside cell arrays and struct fields, and it works with &lt;code&gt;ans&lt;/code&gt; without needing an output variable. It does still turn &lt;code&gt;&quot;&quot;&lt;/code&gt; into a 0x0 empty char array, but that&#039;s fine for my use case.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/string-char-compatibility#comment-431277">Yair Altman</a>.</p>
<p>I was looking up how to do this exact task today. I was about to hold my nose and use the internal controllib function, when I happened to chance across a slightly newer MathWorks function, convertContainedStringsToChars: <a href="https://www.mathworks.com/help/matlab/ref/convertcontainedstringstochars.html" target="_blank" rel="nofollow">https://www.mathworks.com/help/matlab/ref/convertcontainedstringstochars.html</a> </p>
<p>That function recurses in just the way I want it to, and it meets several of your other benefits: it works recursively inside cell arrays and struct fields, and it works with <code>ans</code> without needing an output variable. It does still turn <code>""</code> into a 0x0 empty char array, but that&#8217;s fine for my use case.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/string-char-compatibility#comment-457534</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 16 Jan 2019 09:24:44 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7710#comment-457534</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/string-char-compatibility#comment-457532&quot;&gt;Henry W.H.&lt;/a&gt;.

@Henry - read my response to &lt;a href=&quot;http://undocumentedmatlab.com/blog/string-char-compatibility#comment-431274&quot; rel=&quot;nofollow&quot;&gt;Sam&#039;s comment above&lt;/a&gt;, where I explain the benefits of &lt;i&gt;&lt;b&gt;controllib.internal.util.hString2Char&lt;/b&gt;&lt;/i&gt; over &lt;i&gt;&lt;b&gt;convertStringsToChars&lt;/b&gt;&lt;/i&gt;.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/string-char-compatibility#comment-457532">Henry W.H.</a>.</p>
<p>@Henry &#8211; read my response to <a href="http://undocumentedmatlab.com/blog/string-char-compatibility#comment-431274" rel="nofollow">Sam&#8217;s comment above</a>, where I explain the benefits of <i><b>controllib.internal.util.hString2Char</b></i> over <i><b>convertStringsToChars</b></i>.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Henry W.H.		</title>
		<link>https://undocumentedmatlab.com/articles/string-char-compatibility#comment-457532</link>

		<dc:creator><![CDATA[Henry W.H.]]></dc:creator>
		<pubDate>Wed, 16 Jan 2019 08:58:10 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7710#comment-457532</guid>

					<description><![CDATA[Hi, Yair, I read your blogs a lot. Thanks for sharing all the useful skills and fun tricks.

Regarding the difference between &lt;code&gt;&quot;string&quot;&lt;/code&gt; and &lt;code&gt;&#039;char&#039;&lt;/code&gt;, which did annoy me sometimes. Until recent days, I found that Matlab itself solved this issue by providing the &lt;i&gt;convertStringsToChars&lt;/i&gt; and &lt;i&gt;convertContainedStringsToChars&lt;/i&gt; functions. This page tells the details: &lt;a href=&quot;https://www.mathworks.com/help/matlab/matlab_prog/update-your-code-to-accept-strings.html&quot; target=&quot;_blank&quot; rel=&quot;nofollow&quot;&gt;Update Your Code to Accept Strings&lt;/a&gt;

It helps me, so I &#039;d like to share it with you. Hope it will interest you.]]></description>
			<content:encoded><![CDATA[<p>Hi, Yair, I read your blogs a lot. Thanks for sharing all the useful skills and fun tricks.</p>
<p>Regarding the difference between <code>"string"</code> and <code>'char'</code>, which did annoy me sometimes. Until recent days, I found that Matlab itself solved this issue by providing the <i>convertStringsToChars</i> and <i>convertContainedStringsToChars</i> functions. This page tells the details: <a href="https://www.mathworks.com/help/matlab/matlab_prog/update-your-code-to-accept-strings.html" target="_blank" rel="nofollow">Update Your Code to Accept Strings</a></p>
<p>It helps me, so I &#8216;d like to share it with you. Hope it will interest you.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Gelth		</title>
		<link>https://undocumentedmatlab.com/articles/string-char-compatibility#comment-431793</link>

		<dc:creator><![CDATA[Gelth]]></dc:creator>
		<pubDate>Fri, 06 Jul 2018 09:16:33 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7710#comment-431793</guid>

					<description><![CDATA[@Yair, 
1) Just precision : I just try the command &lt;i&gt;controllib.internal.util.hString2Char(&quot;&quot;)&lt;/i&gt; (with matlab 2017b) and the result was &lt;i&gt;0×0 empty char array&lt;/i&gt;, not &lt;i&gt;&#039;&#039;&lt;/i&gt;.
By looking the M-code, we see &#039;&#039; is reserved for empty string, BUT, &quot;&quot; is scalar String and then the code return &lt;i&gt;char(&quot;&quot;)&lt;/i&gt;, not &#039;&#039;.

2) Since I have problem with tcpip command (thread/ async pbwith big data stream to transfert over tcp) I look at the source code fo tcpip command. And I discover this command :
&lt;i&gt;instrument.internal.stringConversionHelpers.str2char&lt;/i&gt; 

Ok, it is in Instrument Toolbox, but if you have it, it seems best (more clear code than hString2Char because use of &lt;i&gt;switch/case&lt;/i&gt; instead of &lt;i&gt;if/then/else&lt;/i&gt;). Perhaps it should be a good option too.]]></description>
			<content:encoded><![CDATA[<p>@Yair,<br />
1) Just precision : I just try the command <i>controllib.internal.util.hString2Char(&#8220;&#8221;)</i> (with matlab 2017b) and the result was <i>0×0 empty char array</i>, not <i>&#8221;</i>.<br />
By looking the M-code, we see &#8221; is reserved for empty string, BUT, &#8220;&#8221; is scalar String and then the code return <i>char(&#8220;&#8221;)</i>, not &#8221;.</p>
<p>2) Since I have problem with tcpip command (thread/ async pbwith big data stream to transfert over tcp) I look at the source code fo tcpip command. And I discover this command :<br />
<i>instrument.internal.stringConversionHelpers.str2char</i> </p>
<p>Ok, it is in Instrument Toolbox, but if you have it, it seems best (more clear code than hString2Char because use of <i>switch/case</i> instead of <i>if/then/else</i>). Perhaps it should be a good option too.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/string-char-compatibility#comment-431487</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Sun, 01 Jul 2018 14:06:03 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7710#comment-431487</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/string-char-compatibility#comment-431359&quot;&gt;Sam Roberts&lt;/a&gt;.

Sam &amp; David - I don&#039;t want to pounce on the performance issue too much, after all my main point was about robustness and compatibility, not performance (which indeed is use-case dependent). But just for the record, please read &lt;a href=&quot;http://undocumentedmatlab.com/blog/speeding-up-builtin-matlab-functions-part-1#comment-424394&quot; target=&quot;_blank&quot; rel=&quot;nofollow&quot;&gt;Hanan&#039;s recent comment&lt;/a&gt; about how repeated use of &lt;i&gt;&lt;b&gt;verLessThan&lt;/b&gt;&lt;/i&gt; turned out to be a performance bottleneck in his code.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/string-char-compatibility#comment-431359">Sam Roberts</a>.</p>
<p>Sam &#038; David &#8211; I don&#8217;t want to pounce on the performance issue too much, after all my main point was about robustness and compatibility, not performance (which indeed is use-case dependent). But just for the record, please read <a href="http://undocumentedmatlab.com/blog/speeding-up-builtin-matlab-functions-part-1#comment-424394" target="_blank" rel="nofollow">Hanan&#8217;s recent comment</a> about how repeated use of <i><b>verLessThan</b></i> turned out to be a performance bottleneck in his code.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Sam Roberts		</title>
		<link>https://undocumentedmatlab.com/articles/string-char-compatibility#comment-431359</link>

		<dc:creator><![CDATA[Sam Roberts]]></dc:creator>
		<pubDate>Fri, 29 Jun 2018 16:22:41 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7710#comment-431359</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/string-char-compatibility#comment-431283&quot;&gt;David&lt;/a&gt;.

Yair, note that there are cases where your &lt;code&gt;doSomethingWith&lt;/code&gt; will itself slow down, just by virtue of being inside a &lt;code&gt;try/catch&lt;/code&gt; block - for example, if it contains any in-place optimizations (which are disabled when run inside a try/catch block).]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/string-char-compatibility#comment-431283">David</a>.</p>
<p>Yair, note that there are cases where your <code>doSomethingWith</code> will itself slow down, just by virtue of being inside a <code>try/catch</code> block &#8211; for example, if it contains any in-place optimizations (which are disabled when run inside a try/catch block).</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: David		</title>
		<link>https://undocumentedmatlab.com/articles/string-char-compatibility#comment-431353</link>

		<dc:creator><![CDATA[David]]></dc:creator>
		<pubDate>Fri, 29 Jun 2018 14:03:19 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7710#comment-431353</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/string-char-compatibility#comment-431336&quot;&gt;Yair Altman&lt;/a&gt;.

You might want to rethink that statement about version 2 being faster. It&#039;s only slightly faster in the new release where you are effectively doing a needless check. However, it&#039;s a lot slower (total time in my example) in the old release where the error will occur inside the try/catch. If you are trying to support old releases as well then the try/catch could have just introduced a big performance degradation. Obviously all comes down to usecase.

&lt;pre lang=&quot;matlab&quot;&gt;
%% Version 1
nThings = 1e4;
tic;
for iThing = 1:nThings
    if verLessThan(&#039;matlab&#039;, &#039;9.1&#039;)
        % nothing to do in old release
    end
end
toc;

%% Version 2
tic;
for iThing = 1:nThings
    try
        % try to run function in old release which will error because it doesn&#039;t exist
        someFunctionThatWillErrorInOldRelease
    catch
    end
end
toc;
&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/string-char-compatibility#comment-431336">Yair Altman</a>.</p>
<p>You might want to rethink that statement about version 2 being faster. It&#8217;s only slightly faster in the new release where you are effectively doing a needless check. However, it&#8217;s a lot slower (total time in my example) in the old release where the error will occur inside the try/catch. If you are trying to support old releases as well then the try/catch could have just introduced a big performance degradation. Obviously all comes down to usecase.</p>
<pre lang="matlab">
%% Version 1
nThings = 1e4;
tic;
for iThing = 1:nThings
    if verLessThan('matlab', '9.1')
        % nothing to do in old release
    end
end
toc;

%% Version 2
tic;
for iThing = 1:nThings
    try
        % try to run function in old release which will error because it doesn't exist
        someFunctionThatWillErrorInOldRelease
    catch
    end
end
toc;
</pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Yair Altman		</title>
		<link>https://undocumentedmatlab.com/articles/string-char-compatibility#comment-431336</link>

		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Fri, 29 Jun 2018 11:25:40 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7710#comment-431336</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/string-char-compatibility#comment-431287&quot;&gt;David&lt;/a&gt;.

David - I can understand where you&#039;re coming from, but my experience with the code that I write is different. Consider the following code snippet for example:
&lt;pre lang=&quot;matlab&quot;&gt;
% Version 1:
if someCondition()
   data = doSomethingWith(data);
else
   % do nothing in this specific case because...
end

% Version 2:
try
   data = doSomethingWith(data);
catch
   % do nothing in this specific case because...
end
&lt;/pre&gt;

In this case, Version 1 might fail to do the expected thing if you have a problem (bug or exception) in either &lt;code&gt;someCondition()&lt;/code&gt; or &lt;code&gt;doSomethingWith()&lt;/code&gt;. On the other hand, Version 2 will, in the worst case, keep &lt;code&gt;data&lt;/code&gt; unchanged and then proceed with the rest of the downstream processing. For many (but certainly not all) use-cases this would be better than erroring-out on an exception, as in Version 1. Version 2 is also faster than Version 1 due to the avoided condition checks (some checks could take a noticeable time to execute).

Perhaps the question is a matter of expertise level: just as inexperienced drivers should be much more careful in their driving than experienced ones, so too should inexperienced programmers be more careful in their programming while experienced developers have more leeway.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/string-char-compatibility#comment-431287">David</a>.</p>
<p>David &#8211; I can understand where you&#8217;re coming from, but my experience with the code that I write is different. Consider the following code snippet for example:</p>
<pre lang="matlab">
% Version 1:
if someCondition()
   data = doSomethingWith(data);
else
   % do nothing in this specific case because...
end

% Version 2:
try
   data = doSomethingWith(data);
catch
   % do nothing in this specific case because...
end
</pre>
<p>In this case, Version 1 might fail to do the expected thing if you have a problem (bug or exception) in either <code>someCondition()</code> or <code>doSomethingWith()</code>. On the other hand, Version 2 will, in the worst case, keep <code>data</code> unchanged and then proceed with the rest of the downstream processing. For many (but certainly not all) use-cases this would be better than erroring-out on an exception, as in Version 1. Version 2 is also faster than Version 1 due to the avoided condition checks (some checks could take a noticeable time to execute).</p>
<p>Perhaps the question is a matter of expertise level: just as inexperienced drivers should be much more careful in their driving than experienced ones, so too should inexperienced programmers be more careful in their programming while experienced developers have more leeway.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Siyi Deng		</title>
		<link>https://undocumentedmatlab.com/articles/string-char-compatibility#comment-431292</link>

		<dc:creator><![CDATA[Siyi Deng]]></dc:creator>
		<pubDate>Thu, 28 Jun 2018 22:21:27 +0000</pubDate>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7710#comment-431292</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://undocumentedmatlab.com/articles/string-char-compatibility#comment-431287&quot;&gt;David&lt;/a&gt;.

It&#039;s Easier To Ask Forgiveness Than To Get Permission.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://undocumentedmatlab.com/articles/string-char-compatibility#comment-431287">David</a>.</p>
<p>It&#8217;s Easier To Ask Forgiveness Than To Get Permission.</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
