Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Problem with the use of eval in for loop

Subject: Problem with the use of eval in for loop

From: ThomasKUL Knaepen

Date: 22 Nov, 2009 10:04:05

Message: 1 of 6


Dear all,

In most of my programs I tend to use the eval function a lot, especially in for loops in combination with multiple variables.

However it seems that you can't use eval when the command has a ' ' part in it. (Like the case with the imwrite function, or the exist function: A = exist('name','kind') ).

Here's an example of the problem I'm having:
%%%%
for j=1:2
    
eval([ 'imwrite(Im_ ' num2str(j) ', ''Sample100_CELLS_v000' num2str(j) '.jpg'');']);

end
%%%%
So I wish to save two images (Im_1 & Im_2) in a certain directory. However there is a problem, which I can't seem to fix, with the ' ', because imwrite has in it's second part between the brackets a mandatory use of ' '.

How can I circumvent this problem. I need to save the images at the end of my program, that's all. But I want it all to happen automaticly offcourse(with a for loop).

Any thoughts?

Thanks alot!

Kind regards,

Thomas

Subject: Problem with the use of eval in for loop

From: Miroslav Balda

Date: 22 Nov, 2009 11:40:09

Message: 2 of 6

"ThomasKUL Knaepen" <xhearts.alive@gmail.com> wrote in message <heb2ak$krv$1@fred.mathworks.com>...
SNIP
> Thomas

Hi Thomas,
maybe that you do not to use eval at all, if you use
   imwrite([Im_ ' num2str(j) '], ['Sample100_CELLS_v000' num2str(j)], '.jpg');
in the for cycle.
Mira

Subject: Problem with the use of eval in for loop

From: Jan Simon

Date: 22 Nov, 2009 11:45:27

Message: 3 of 6

Dear Thomas!

> In most of my programs I tend to use the eval function a lot, especially in for loops in combination with multiple variables.
>
> However it seems that you can't use eval when the command has a ' ' part in it. (Like the case with the imwrite function, or the exist function: A = exist('name','kind') ).
>
> Here's an example of the problem I'm having:
> %%%%
> for j=1:2
> eval([ 'imwrite(Im_ ' num2str(j) ', ''Sample100_CELLS_v000' num2str(j) '.jpg'');']);
> end
> %%%%
> So I wish to save two images (Im_1 & Im_2) in a certain directory. However there is a problem, which I can't seem to fix, with the ' ', because imwrite has in it's second part between the brackets a mandatory use of ' '.
>
> How can I circumvent this problem. I need to save the images at the end of my program, that's all. But I want it all to happen automaticly offcourse(with a for loop).

There is no problem with EVAL and spaces. If your EXIST function does not work in an EVAL call, there is another reason than the space.

Nevertheless, I really recommend to avoid the EVAL. At first, see the corresponding FAQs, e.g. search here in the newsgroup for 'eval'.
At second, you can limit the EVAL to the necessary minimum:
  for j = 1:2
    data = eval(sprintf('Im_%d', j));
    imwrite(data, sprintf('Sample100_CELLS_v000%d.jpg', j));
  end

But it would be much faster and safe to avoid storing the image data in a set of variables Im_1, Im_2, ... Use a cell instead:
  Im_data = cell(1, 2);
  Im_data{1} = ...
  Im_data{2} = ...

  for j=1:2
    imwrite(Im_data{j}, sprintf('Sample100_CELLS_v000%d.jpg', j));
  end

Eval is the safest method to impede, that Matlab can process the source efficiently.
Kind regards, Jan

Subject: Problem with the use of eval in for loop

From: Matt

Date: 22 Nov, 2009 13:37:05

Message: 4 of 6

"ThomasKUL Knaepen" <xhearts.alive@gmail.com> wrote in message <heb2ak$krv$1@fred.mathworks.com>...
>
> Dear all,
>
> In most of my programs I tend to use the eval function a lot, especially in for loops in combination with multiple variables.
>
> However it seems that you can't use eval when the command has a ' ' part in it. (Like the case with the imwrite function, or the exist function: A = exist('name','kind') ).
>
> Here's an example of the problem I'm having:
> %%%%
> for j=1:2
>
> eval([ 'imwrite(Im_ ' num2str(j) ', ''Sample100_CELLS_v000' num2str(j) '.jpg'');']);
>

To represent a quote as a character string, use ''. So the above would be

eval([ 'imwrite(Im_ ' num2str(j) ', '''mple100_CELLS_v000' num2str(j) '.jpg''');']);

Subject: Problem with the use of eval in for loop

From: ThomasKUL Knaepen

Date: 23 Nov, 2009 11:09:03

Message: 5 of 6


Dear Jan,

Thank you so much. Really, you gave me a new insight on the use of eval, I replaced all my evals with cells, and my algorithm got a lot (!!) faster!

Thanks,

Thomas



"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <heb88n$77o$1@fred.mathworks.com>...
> Dear Thomas!
>
> > In most of my programs I tend to use the eval function a lot, especially in for loops in combination with multiple variables.
> >
> > However it seems that you can't use eval when the command has a ' ' part in it. (Like the case with the imwrite function, or the exist function: A = exist('name','kind') ).
> >
> > Here's an example of the problem I'm having:
> > %%%%
> > for j=1:2
> > eval([ 'imwrite(Im_ ' num2str(j) ', ''Sample100_CELLS_v000' num2str(j) '.jpg'');']);
> > end
> > %%%%
> > So I wish to save two images (Im_1 & Im_2) in a certain directory. However there is a problem, which I can't seem to fix, with the ' ', because imwrite has in it's second part between the brackets a mandatory use of ' '.
> >
> > How can I circumvent this problem. I need to save the images at the end of my program, that's all. But I want it all to happen automaticly offcourse(with a for loop).
>
> There is no problem with EVAL and spaces. If your EXIST function does not work in an EVAL call, there is another reason than the space.
>
> Nevertheless, I really recommend to avoid the EVAL. At first, see the corresponding FAQs, e.g. search here in the newsgroup for 'eval'.
> At second, you can limit the EVAL to the necessary minimum:
> for j = 1:2
> data = eval(sprintf('Im_%d', j));
> imwrite(data, sprintf('Sample100_CELLS_v000%d.jpg', j));
> end
>
> But it would be much faster and safe to avoid storing the image data in a set of variables Im_1, Im_2, ... Use a cell instead:
> Im_data = cell(1, 2);
> Im_data{1} = ...
> Im_data{2} = ...
>
> for j=1:2
> imwrite(Im_data{j}, sprintf('Sample100_CELLS_v000%d.jpg', j));
> end
>
> Eval is the safest method to impede, that Matlab can process the source efficiently.
> Kind regards, Jan

Subject: Problem with the use of eval in for loop

From: Jan Simon

Date: 23 Nov, 2009 11:30:22

Message: 6 of 6

Dear Thomas!

> I replaced all my evals with cells, and my algorithm got a lot (!!) faster!
> Thomas

Welcome to the world of fast Matlab processing!
Happy to have you here :-)
Jan

Tags for this Thread

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.

Contact us