<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264719</link>
    <title>MATLAB Central Newsreader - EVAL within EVAL</title>
    <description>Feed for thread: EVAL within EVAL</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Mon, 02 Nov 2009 01:40:19 -0500</pubDate>
      <title>EVAL within EVAL</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264719#691384</link>
      <author>Ariel Krieger</author>
      <description>Hello all,&lt;br&gt;
&lt;br&gt;
I'm trying to do something very basic with EVAL but not getting there.&lt;br&gt;
&lt;br&gt;
So I have a set of vectors x1...xn and in a for loop I want to perform some arithmatic on them.&lt;br&gt;
&lt;br&gt;
I use EVAL to create them automatically with the proper name and all:&lt;br&gt;
&lt;br&gt;
for i = 1:10&lt;br&gt;
eval(['x' num2str(i) ' = ones(1,100)']);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
that creates vectors x1, x2, ..., x10 which are 1 X 100 row vectors containing ones.&lt;br&gt;
&lt;br&gt;
Now the problem is I want to do something like this (just for example):&lt;br&gt;
&lt;br&gt;
x1 = x1 * 3;&lt;br&gt;
x2 = x2 * 3;&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
&lt;br&gt;
can I use eval within eval? I'm trying something like:&lt;br&gt;
&lt;br&gt;
for i = 1: 10&lt;br&gt;
eval(['x' num2str(i) ' = eval(['x' num2str(i) ' ']) * 3']);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
but obviously that doesn't fly, any suggestions?&lt;br&gt;
&lt;br&gt;
Thanks</description>
    </item>
    <item>
      <pubDate>Mon, 02 Nov 2009 03:01:48 -0500</pubDate>
      <title>Re: EVAL within EVAL</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264719#691388</link>
      <author>ImageAnalyst</author>
      <description>Why do it that strange way?  Why not simply do&lt;br&gt;
x = ones(10, 100);&lt;br&gt;
x = x * 3;&lt;br&gt;
or even&lt;br&gt;
x = 3 * ones(10, 100)&lt;br&gt;
&lt;br&gt;
That way you're not going against the recommendations of section 4.6&lt;br&gt;
of the FAQ:&lt;br&gt;
&lt;a href=&quot;http://matlabwiki.mathworks.com/MATLAB_FAQ&quot;&gt;http://matlabwiki.mathworks.com/MATLAB_FAQ&lt;/a&gt;</description>
    </item>
    <item>
      <pubDate>Mon, 02 Nov 2009 03:19:01 -0500</pubDate>
      <title>Re: EVAL within EVAL</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264719#691390</link>
      <author>Ariel Krieger</author>
      <description>ImageAnalyst &amp;lt;imageanalyst@mailinator.com&amp;gt; wrote in message &amp;lt;f94955c1-38ae-48ba-a617-984604e29e1a@j11g2000vbi.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; Why do it that strange way?  Why not simply do&lt;br&gt;
&amp;gt; x = ones(10, 100);&lt;br&gt;
&amp;gt; x = x * 3;&lt;br&gt;
&amp;gt; or even&lt;br&gt;
&amp;gt; x = 3 * ones(10, 100)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; That way you're not going against the recommendations of section 4.6&lt;br&gt;
&amp;gt; of the FAQ:&lt;br&gt;
&amp;gt; &lt;a href=&quot;http://matlabwiki.mathworks.com/MATLAB_FAQ&quot;&gt;http://matlabwiki.mathworks.com/MATLAB_FAQ&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
sorry the example I gave was extremely dumb, that's not what I want to do it was just an easy way to explain my intentions. I'll try to be more coherent:&lt;br&gt;
&lt;br&gt;
Let's say I have 100 matrices N1, N2, ... N100&lt;br&gt;
now, with a loop I'd like to remove all the zero rows from them.&lt;br&gt;
for one matrix this would be done pretty easily:&lt;br&gt;
N1(~any(N1,2),:) = [];&lt;br&gt;
&lt;br&gt;
how would I go about doing that for all the Ns?&lt;br&gt;
&lt;br&gt;
hope I'm more clear,&lt;br&gt;
&lt;br&gt;
thanks</description>
    </item>
    <item>
      <pubDate>Mon, 02 Nov 2009 03:27:54 -0500</pubDate>
      <title>Re: EVAL within EVAL</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264719#691391</link>
      <author>ImageAnalyst</author>
      <description>Do you want to just get the job done, or be fancy/tricky about it?  If&lt;br&gt;
you know the number and names of the arrays in advance, and you just&lt;br&gt;
want to get it done, then copy and paste works pretty well.&lt;br&gt;
N1(~any(N1,2),:) = [];&lt;br&gt;
N2(~any(N2,2),:) = [];&lt;br&gt;
N3(~any(N3,2),:) = [];&lt;br&gt;
N4(~any(N4,2),:) = [];&lt;br&gt;
N5(~any(N5,2),:) = [];&lt;br&gt;
and so on up to N100.&lt;br&gt;
You could probably do all 100 lines in 2 or 3 minutes.</description>
    </item>
    <item>
      <pubDate>Mon, 02 Nov 2009 03:34:02 -0500</pubDate>
      <title>Re: EVAL within EVAL</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264719#691392</link>
      <author>Ariel Krieger</author>
      <description>ImageAnalyst &amp;lt;imageanalyst@mailinator.com&amp;gt; wrote in message &amp;lt;c18aae70-b609-444f-9afa-cb27576d39f9@l13g2000yqb.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; Do you want to just get the job done, or be fancy/tricky about it?  If&lt;br&gt;
&amp;gt; you know the number and names of the arrays in advance, and you just&lt;br&gt;
&amp;gt; want to get it done, then copy and paste works pretty well.&lt;br&gt;
&amp;gt; N1(~any(N1,2),:) = [];&lt;br&gt;
&amp;gt; N2(~any(N2,2),:) = [];&lt;br&gt;
&amp;gt; N3(~any(N3,2),:) = [];&lt;br&gt;
&amp;gt; N4(~any(N4,2),:) = [];&lt;br&gt;
&amp;gt; N5(~any(N5,2),:) = [];&lt;br&gt;
&amp;gt; and so on up to N100.&lt;br&gt;
&amp;gt; You could probably do all 100 lines in 2 or 3 minutes.&lt;br&gt;
&lt;br&gt;
unfortunately I don't know in advance how many of these matrices there will be, it's user dependent. believe me, I'm not ashamed of copy/pasting :)</description>
    </item>
    <item>
      <pubDate>Mon, 02 Nov 2009 03:43:29 -0500</pubDate>
      <title>Re: EVAL within EVAL</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264719#691393</link>
      <author>ImageAnalyst</author>
      <description>Then how about this?  You can probably adapt this to your real needs:&lt;br&gt;
&lt;br&gt;
clc;&lt;br&gt;
close all;&lt;br&gt;
clear all;&lt;br&gt;
workspace; % Show the Workspace panel.&lt;br&gt;
% Make up some random names that DO exist.&lt;br&gt;
N1 = ones(1,10);&lt;br&gt;
N20 = rand(1, 15);&lt;br&gt;
N42 = ones(1,10);&lt;br&gt;
N69 = rand(1, 16);&lt;br&gt;
% The other ones don't exist.&lt;br&gt;
for k = 1:500 % to the biggest you ever expect to have.&lt;br&gt;
	varName = sprintf('N%d', k);&lt;br&gt;
	if exist(varName, 'var')&lt;br&gt;
		fprintf(1, '%s exists!\n', varName);&lt;br&gt;
		commandLine = sprintf('%s(~any(%s,2),:) = [];', varName, varName);&lt;br&gt;
		eval(commandLine);&lt;br&gt;
		eval(varName); % Display new values in command window.&lt;br&gt;
	else&lt;br&gt;
		fprintf(1, '%s does not exist.\n', varName);&lt;br&gt;
	end&lt;br&gt;
end</description>
    </item>
    <item>
      <pubDate>Mon, 02 Nov 2009 11:52:02 -0500</pubDate>
      <title>Re: EVAL within EVAL</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264719#691458</link>
      <author>Jan Simon</author>
      <description>Dear Ariel!&lt;br&gt;
&lt;br&gt;
I really recommend to find a better way than EVAL. E.g. use a struct with the field names 'x1', 'x2' etc:&lt;br&gt;
&amp;nbsp;&amp;nbsp;for i = 1:10&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;name = sprintf('x%d', i);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;S.(name) = S.(name) * 3;&lt;br&gt;
&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&lt;br&gt;
If all vectors have the same size, better use a Matrix to store them.&lt;br&gt;
If the vectors have different length, a cell is much better than EVAL.&lt;br&gt;
&lt;br&gt;
Anyway. You asked for:&lt;br&gt;
&lt;br&gt;
&amp;gt; for i = 1: 10&lt;br&gt;
&amp;gt;   eval(['x' num2str(i) ' = eval(['x' num2str(i) ' ']) * 3']);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&lt;br&gt;
This would work as:&lt;br&gt;
&amp;nbsp;for i = 1: 10&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;eval(['x', sprintf('%d', i), ' = x', sprintf('%d', i), ' * 3']);&lt;br&gt;
&amp;nbsp;end&lt;br&gt;
&lt;br&gt;
SPRINTF is faster than NUM2STR. But if you start to use it, your EVAL line can be even simplified:&lt;br&gt;
&amp;nbsp;for i = 1: 10&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;eval(sprintf('x%d = x%d * 3', i, i));&lt;br&gt;
&amp;nbsp;end&lt;br&gt;
&lt;br&gt;
But, as said alread, non-EVAL methods are better, Jan</description>
    </item>
    <item>
      <pubDate>Mon, 02 Nov 2009 12:49:52 -0500</pubDate>
      <title>Re: EVAL within EVAL</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264719#691475</link>
      <author>dpb</author>
      <description>Ariel Krieger wrote:&lt;br&gt;
...&lt;br&gt;
&amp;gt; unfortunately I don't know in advance how many of these matrices&lt;br&gt;
&amp;gt; there will be, it's user dependent. believe me, I'm not ashamed of&lt;br&gt;
&amp;gt; copy/pasting :)&lt;br&gt;
&lt;br&gt;
Have you yet read the FAQ on how to avoid spoofing variables?  It'll &lt;br&gt;
make your life much simpler in general... :)&lt;br&gt;
&lt;br&gt;
--</description>
    </item>
    <item>
      <pubDate>Mon, 02 Nov 2009 13:11:20 -0500</pubDate>
      <title>Re: EVAL within EVAL</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264719#691479</link>
      <author>ImageAnalyst</author>
      <description>I got the impression that he was just stuck with the situation.  For&lt;br&gt;
example the variables are already in existence from running someone&lt;br&gt;
else's code (and he can't change the situation unless he undergoes a&lt;br&gt;
major rewrite of someone else's code - always a delight), or perhaps&lt;br&gt;
reading them in from a mat file over which he has no control over.&lt;br&gt;
Ariel, do you actually have control over how these variables come into&lt;br&gt;
existence, or are you just stuck with them and have to figure out how&lt;br&gt;
to deal with them?</description>
    </item>
    <item>
      <pubDate>Mon, 02 Nov 2009 13:37:39 -0500</pubDate>
      <title>Re: EVAL within EVAL</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264719#691491</link>
      <author>dpb</author>
      <description>ImageAnalyst wrote:&lt;br&gt;
&amp;gt; I got the impression that he was just stuck with the situation.  ...&lt;br&gt;
&lt;br&gt;
Ahhh...well, perhaps, that didn't occur to me, actually; I thought was &lt;br&gt;
just probably the mindset of &quot;Started down this road and going to &lt;br&gt;
finish, by golly...&quot;...  :)&lt;br&gt;
&lt;br&gt;
--</description>
    </item>
    <item>
      <pubDate>Mon, 02 Nov 2009 20:32:03 -0500</pubDate>
      <title>Re: EVAL within EVAL</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264719#691580</link>
      <author>Wendy Birdsong</author>
      <description>&quot;Ariel Krieger&quot; &amp;lt;srigi001@gmail.com&amp;gt; wrote in message &amp;lt;hclda3$slq$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hello all,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I'm trying to do something very basic with EVAL but not getting there.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So I have a set of vectors x1...xn and in a for loop I want to perform some arithmatic on them.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I use EVAL to create them automatically with the proper name and all:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for i = 1:10&lt;br&gt;
&amp;gt; eval(['x' num2str(i) ' = ones(1,100)']);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; that creates vectors x1, x2, ..., x10 which are 1 X 100 row vectors containing ones.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Now the problem is I want to do something like this (just for example):&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; x1 = x1 * 3;&lt;br&gt;
&amp;gt; x2 = x2 * 3;&lt;br&gt;
&amp;gt; .&lt;br&gt;
&amp;gt; .&lt;br&gt;
&amp;gt; .&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; can I use eval within eval? I'm trying something like:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for i = 1: 10&lt;br&gt;
&amp;gt; eval(['x' num2str(i) ' = eval(['x' num2str(i) ' ']) * 3']);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; but obviously that doesn't fly, any suggestions?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks&lt;br&gt;
&lt;br&gt;
Try using &lt;br&gt;
&lt;br&gt;
MatrixNames = whos('N*')&lt;br&gt;
for ii=1:length(MatrixNames)&lt;br&gt;
eval([MatrixNames(ii).name '=' MatrixNames(ii).name '*3'])&lt;br&gt;
end</description>
    </item>
  </channel>
</rss>

