<?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: Evading eval</title>
	<atom:link href="http://blogs.mathworks.com/loren/2005/12/28/evading-eval/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2005/12/28/evading-eval/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Thu, 09 Feb 2012 04:19:21 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Martin</title>
		<link>http://blogs.mathworks.com/loren/2005/12/28/evading-eval/#comment-31969</link>
		<dc:creator>Martin</dc:creator>
		<pubDate>Mon, 17 Jan 2011 09:49:04 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=9#comment-31969</guid>
		<description>Hi Steve,

I of course realized immediately that using cell arrays is much more flexible programming. Thanks for your comment!

Martin</description>
		<content:encoded><![CDATA[<p>Hi Steve,</p>
<p>I of course realized immediately that using cell arrays is much more flexible programming. Thanks for your comment!</p>
<p>Martin</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve L</title>
		<link>http://blogs.mathworks.com/loren/2005/12/28/evading-eval/#comment-31948</link>
		<dc:creator>Steve L</dc:creator>
		<pubDate>Fri, 07 Jan 2011 18:55:07 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=9#comment-31948</guid>
		<description>Martin,

Creating variables named s1, s2, s3, etc. is generally a bad idea for several reasons, some of which Loren called out in her blog post and others of which are discussed in the Programming section of the comp.soft-sys.matlab FAQ:

http://matlab.wikia.com/wiki/FAQ

I would use a cell array for this task.

&lt;pre&gt;
s = cell(1, 10);
for k = 1:10
    s{k} = sprintf(&#039;file%d&#039;, k);
% or
%   s{k} = [&#039;file&#039; num2str(k)];
end
&lt;/pre&gt;

To extract one of the strings (for example, &#039;file5&#039;) from the cell array use curly brace indexing:

&lt;pre&gt;
myfilename = s{5}
&lt;/pre&gt;

or you could iterate over the cells in the cell array using a FOR loop or the CELLFUN function.

&lt;pre&gt;
for k = 1:numel(s)
    fprintf(&#039;String number %d is %s.\n&#039;, k, s{k});
end
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Martin,</p>
<p>Creating variables named s1, s2, s3, etc. is generally a bad idea for several reasons, some of which Loren called out in her blog post and others of which are discussed in the Programming section of the comp.soft-sys.matlab FAQ:</p>
<p><a href="http://matlab.wikia.com/wiki/FAQ" rel="nofollow">http://matlab.wikia.com/wiki/FAQ</a></p>
<p>I would use a cell array for this task.</p>
<pre>
s = cell(1, 10);
for k = 1:10
    s{k} = sprintf('file%d', k);
% or
%   s{k} = ['file' num2str(k)];
end
</pre>
<p>To extract one of the strings (for example, &#8216;file5&#8242;) from the cell array use curly brace indexing:</p>
<pre>
myfilename = s{5}
</pre>
<p>or you could iterate over the cells in the cell array using a FOR loop or the CELLFUN function.</p>
<pre>
for k = 1:numel(s)
    fprintf('String number %d is %s.\n', k, s{k});
end
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Martin</title>
		<link>http://blogs.mathworks.com/loren/2005/12/28/evading-eval/#comment-31947</link>
		<dc:creator>Martin</dc:creator>
		<pubDate>Fri, 07 Jan 2011 13:42:37 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=9#comment-31947</guid>
		<description>Hi Loren &amp; list,

I have used EVAL to perform the following loop statement to create n variables named s1, s2, ..., etc. containing a string

&lt;pre&gt;
for i = 1:10
eval([&#039;s&#039; num2str(i) &#039;=&#039; &#039;file&#039;])
end

&lt;/pre&gt;


but what I really want to do is name the strings &#039;file1&#039;, 
&#039;file2&#039;, ..., etc. which does not seem to work with EVAL

&lt;pre&gt;

for i = 1:10
eval([&#039;s&#039; num2str(i) &#039;=&#039; &#039;file&#039; num2str(i)])
end

&lt;/pre&gt;

 
Do you have any suggestions for avoiding EVAL ?

Best,
Martin</description>
		<content:encoded><![CDATA[<p>Hi Loren &amp; list,</p>
<p>I have used EVAL to perform the following loop statement to create n variables named s1, s2, &#8230;, etc. containing a string</p>
<pre>
for i = 1:10
eval(['s' num2str(i) '=' 'file'])
end
</pre>
<p>but what I really want to do is name the strings &#8216;file1&#8242;,<br />
&#8216;file2&#8242;, &#8230;, etc. which does not seem to work with EVAL</p>
<pre>

for i = 1:10
eval(['s' num2str(i) '=' 'file' num2str(i)])
end
</pre>
<p>Do you have any suggestions for avoiding EVAL ?</p>
<p>Best,<br />
Martin</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Simon</title>
		<link>http://blogs.mathworks.com/loren/2005/12/28/evading-eval/#comment-31465</link>
		<dc:creator>Simon</dc:creator>
		<pubDate>Sun, 27 Jun 2010 20:46:35 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=9#comment-31465</guid>
		<description>Well, big thank you Loren!

I was a bit desperate because I thought I was forced to use the eval function in my code. And of course the calculation time just skyrocketed...

But now, I know how not to use that lazy function.

Thanks again !</description>
		<content:encoded><![CDATA[<p>Well, big thank you Loren!</p>
<p>I was a bit desperate because I thought I was forced to use the eval function in my code. And of course the calculation time just skyrocketed&#8230;</p>
<p>But now, I know how not to use that lazy function.</p>
<p>Thanks again !</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: George</title>
		<link>http://blogs.mathworks.com/loren/2005/12/28/evading-eval/#comment-30664</link>
		<dc:creator>George</dc:creator>
		<pubDate>Wed, 07 Oct 2009 12:53:51 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=9#comment-30664</guid>
		<description>Thanks for a great article Loren, 
I came across it cause I have been experiencing problems with how terrebly slow the eval function is. 
I have a large code, and thought I could speed it up by avoiding some calculations and using the eval, but the running time exploded!
Lesson:avoid eval by all means :)</description>
		<content:encoded><![CDATA[<p>Thanks for a great article Loren,<br />
I came across it cause I have been experiencing problems with how terrebly slow the eval function is.<br />
I have a large code, and thought I could speed it up by avoiding some calculations and using the eval, but the running time exploded!<br />
Lesson:avoid eval by all means :)</p>
]]></content:encoded>
	</item>
</channel>
</rss>

