Thread Subject: EVAL within EVAL

Subject: EVAL within EVAL

From: Ariel Krieger

Date: 2 Nov, 2009 01:40:19

Message: 1 of 11

Hello all,

I'm trying to do something very basic with EVAL but not getting there.

So I have a set of vectors x1...xn and in a for loop I want to perform some arithmatic on them.

I use EVAL to create them automatically with the proper name and all:

for i = 1:10
eval(['x' num2str(i) ' = ones(1,100)']);
end

that creates vectors x1, x2, ..., x10 which are 1 X 100 row vectors containing ones.

Now the problem is I want to do something like this (just for example):

x1 = x1 * 3;
x2 = x2 * 3;
.
.
.

can I use eval within eval? I'm trying something like:

for i = 1: 10
eval(['x' num2str(i) ' = eval(['x' num2str(i) ' ']) * 3']);
end

but obviously that doesn't fly, any suggestions?

Thanks

Subject: EVAL within EVAL

From: ImageAnalyst

Date: 2 Nov, 2009 03:01:48

Message: 2 of 11

Why do it that strange way? Why not simply do
x = ones(10, 100);
x = x * 3;
or even
x = 3 * ones(10, 100)

That way you're not going against the recommendations of section 4.6
of the FAQ:
http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: EVAL within EVAL

From: Ariel Krieger

Date: 2 Nov, 2009 03:19:01

Message: 3 of 11

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <f94955c1-38ae-48ba-a617-984604e29e1a@j11g2000vbi.googlegroups.com>...
> Why do it that strange way? Why not simply do
> x = ones(10, 100);
> x = x * 3;
> or even
> x = 3 * ones(10, 100)
>
> That way you're not going against the recommendations of section 4.6
> of the FAQ:
> http://matlabwiki.mathworks.com/MATLAB_FAQ

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:

Let's say I have 100 matrices N1, N2, ... N100
now, with a loop I'd like to remove all the zero rows from them.
for one matrix this would be done pretty easily:
N1(~any(N1,2),:) = [];

how would I go about doing that for all the Ns?

hope I'm more clear,

thanks

Subject: EVAL within EVAL

From: ImageAnalyst

Date: 2 Nov, 2009 03:27:54

Message: 4 of 11

Do you want to just get the job done, or be fancy/tricky about it? If
you know the number and names of the arrays in advance, and you just
want to get it done, then copy and paste works pretty well.
N1(~any(N1,2),:) = [];
N2(~any(N2,2),:) = [];
N3(~any(N3,2),:) = [];
N4(~any(N4,2),:) = [];
N5(~any(N5,2),:) = [];
and so on up to N100.
You could probably do all 100 lines in 2 or 3 minutes.

Subject: EVAL within EVAL

From: Ariel Krieger

Date: 2 Nov, 2009 03:34:02

Message: 5 of 11

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <c18aae70-b609-444f-9afa-cb27576d39f9@l13g2000yqb.googlegroups.com>...
> Do you want to just get the job done, or be fancy/tricky about it? If
> you know the number and names of the arrays in advance, and you just
> want to get it done, then copy and paste works pretty well.
> N1(~any(N1,2),:) = [];
> N2(~any(N2,2),:) = [];
> N3(~any(N3,2),:) = [];
> N4(~any(N4,2),:) = [];
> N5(~any(N5,2),:) = [];
> and so on up to N100.
> You could probably do all 100 lines in 2 or 3 minutes.

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 :)

Subject: EVAL within EVAL

From: ImageAnalyst

Date: 2 Nov, 2009 03:43:29

Message: 6 of 11

Then how about this? You can probably adapt this to your real needs:

clc;
close all;
clear all;
workspace; % Show the Workspace panel.
% Make up some random names that DO exist.
N1 = ones(1,10);
N20 = rand(1, 15);
N42 = ones(1,10);
N69 = rand(1, 16);
% The other ones don't exist.
for k = 1:500 % to the biggest you ever expect to have.
varName = sprintf('N%d', k);
if exist(varName, 'var')
fprintf(1, '%s exists!\n', varName);
commandLine = sprintf('%s(~any(%s,2),:) = [];', varName, varName);
eval(commandLine);
eval(varName); % Display new values in command window.
else
fprintf(1, '%s does not exist.\n', varName);
end
end

Subject: EVAL within EVAL

From: Jan Simon

Date: 2 Nov, 2009 11:52:02

Message: 7 of 11

Dear Ariel!

I really recommend to find a better way than EVAL. E.g. use a struct with the field names 'x1', 'x2' etc:
  for i = 1:10
    name = sprintf('x%d', i);
    S.(name) = S.(name) * 3;
  end

If all vectors have the same size, better use a Matrix to store them.
If the vectors have different length, a cell is much better than EVAL.

Anyway. You asked for:

> for i = 1: 10
> eval(['x' num2str(i) ' = eval(['x' num2str(i) ' ']) * 3']);
> end

This would work as:
 for i = 1: 10
   eval(['x', sprintf('%d', i), ' = x', sprintf('%d', i), ' * 3']);
 end

SPRINTF is faster than NUM2STR. But if you start to use it, your EVAL line can be even simplified:
 for i = 1: 10
   eval(sprintf('x%d = x%d * 3', i, i));
 end

But, as said alread, non-EVAL methods are better, Jan

Subject: EVAL within EVAL

From: dpb

Date: 2 Nov, 2009 12:49:52

Message: 8 of 11

Ariel Krieger wrote:
...
> 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 :)

Have you yet read the FAQ on how to avoid spoofing variables? It'll
make your life much simpler in general... :)

--

Subject: EVAL within EVAL

From: ImageAnalyst

Date: 2 Nov, 2009 13:11:20

Message: 9 of 11

I got the impression that he was just stuck with the situation. For
example the variables are already in existence from running someone
else's code (and he can't change the situation unless he undergoes a
major rewrite of someone else's code - always a delight), or perhaps
reading them in from a mat file over which he has no control over.
Ariel, do you actually have control over how these variables come into
existence, or are you just stuck with them and have to figure out how
to deal with them?

Subject: EVAL within EVAL

From: dpb

Date: 2 Nov, 2009 13:37:39

Message: 10 of 11

ImageAnalyst wrote:
> I got the impression that he was just stuck with the situation. ...

Ahhh...well, perhaps, that didn't occur to me, actually; I thought was
just probably the mindset of "Started down this road and going to
finish, by golly..."... :)

--

Subject: EVAL within EVAL

From: Wendy Birdsong

Date: 2 Nov, 2009 20:32:03

Message: 11 of 11

"Ariel Krieger" <srigi001@gmail.com> wrote in message <hclda3$slq$1@fred.mathworks.com>...
> Hello all,
>
> I'm trying to do something very basic with EVAL but not getting there.
>
> So I have a set of vectors x1...xn and in a for loop I want to perform some arithmatic on them.
>
> I use EVAL to create them automatically with the proper name and all:
>
> for i = 1:10
> eval(['x' num2str(i) ' = ones(1,100)']);
> end
>
> that creates vectors x1, x2, ..., x10 which are 1 X 100 row vectors containing ones.
>
> Now the problem is I want to do something like this (just for example):
>
> x1 = x1 * 3;
> x2 = x2 * 3;
> .
> .
> .
>
> can I use eval within eval? I'm trying something like:
>
> for i = 1: 10
> eval(['x' num2str(i) ' = eval(['x' num2str(i) ' ']) * 3']);
> end
>
> but obviously that doesn't fly, any suggestions?
>
> Thanks

Try using

MatrixNames = whos('N*')
for ii=1:length(MatrixNames)
eval([MatrixNames(ii).name '=' MatrixNames(ii).name '*3'])
end

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com