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:
Use eval() to execute for loops and if clauses

Subject: Use eval() to execute for loops and if clauses

From: Enrico

Date: 4 Oct, 2013 10:18:06

Message: 1 of 5

Hi all,

I have a code that writes a string depending on what happened before in the program. The string created is more MATLAB code possibly containing for loops or if clauses.

I want to execute the string as if it were MATLAB code, i tried to use the eval() function but it did not work out.

A simple example is:

a=5;
eval('if a>4 b=1 end');

MATLAB returns the error:

Error: Illegal use of reserved keyword "end".

The same thing happens with for loops. Does anybody know how to bypass the problem? Maybe another function I am not aware of?

Thank you!

Subject: Use eval() to execute for loops and if clauses

From: Christopher Creutzig

Date: 4 Oct, 2013 12:43:19

Message: 2 of 5

On 04.10.13 12:18, Enrico wrote:

> I have a code that writes a string depending on what happened before in the program. The string created is more MATLAB code possibly containing for loops or if clauses.

I think you should seriously consider solving your problem in a
different way. Creating code on they fly is usually not a good idea.

> I want to execute the string as if it were MATLAB code, i tried to use the eval() function but it did not work out.
>
> A simple example is:
>
> a=5;
> eval('if a>4 b=1 end');
>
> MATLAB returns the error:
>
> Error: Illegal use of reserved keyword "end".

Which is exactly what happens if the string were MATLAB code outside of
eval, so it did just what you said you wanted:

>> a=5;
>> if a>4 b=1 end
 if a>4 b=1 end
            |
Error: Illegal use of reserved keyword "end".

Maybe you actually wanted to do something like this instead?

>> if a>4, b=1, end

b =

     1


But again, try not to use eval. Even if you ignore the
debugging/maintaining problems with it, eval will very likely be slower
than other routes to your original problem.


HTH,

Christopher

Subject: Use eval() to execute for loops and if clauses

From: Enrico

Date: 4 Oct, 2013 14:05:07

Message: 3 of 5

Christopher Creutzig <Christopher.Creutzig@mathworks.com> wrote in message <524EB7E7.4070708@mathworks.com>...
> On 04.10.13 12:18, Enrico wrote:
>
> > I have a code that writes a string depending on what happened before in the program. The string created is more MATLAB code possibly containing for loops or if clauses.
>
> I think you should seriously consider solving your problem in a
> different way. Creating code on they fly is usually not a good idea.
>
> > I want to execute the string as if it were MATLAB code, i tried to use the eval() function but it did not work out.
> >
> > A simple example is:
> >
> > a=5;
> > eval('if a>4 b=1 end');
> >
> > MATLAB returns the error:
> >
> > Error: Illegal use of reserved keyword "end".
>
> Which is exactly what happens if the string were MATLAB code outside of
> eval, so it did just what you said you wanted:
>
> >> a=5;
> >> if a>4 b=1 end
> if a>4 b=1 end
> |
> Error: Illegal use of reserved keyword "end".
>
> Maybe you actually wanted to do something like this instead?
>
> >> if a>4, b=1, end
>
> b =
>
> 1
>
>
> But again, try not to use eval. Even if you ignore the
> debugging/maintaining problems with it, eval will very likely be slower
> than other routes to your original problem.
>
>
> HTH,
>
> Christopher


Thank you very much Christopher, it works!

I know it's a bad habit to use the eval function but I have no other ideas to solve my problem.

Very briefly.....

I have to run a specific function that backtests decisions over time series. The function receives parameters from the user and produces a quantity over these parameters. I then optimize these quantities running a grid search. Given a number of user-defined conditions (string) and the set of parameters (matrix) I test every possible combination of the parameters (using nested loops...I know, curse of dimensionality).

The difficulty is that I do NOT know in advance how many nested loops will be needed to run the search (1, 2, 100?), it will depend on the number of conditions the user sets. So, what I do know is analyze the string the user sets, create ad hoc code, and execute it using eval.

If you have any better ideas, please share

Thanks!

Enrico

Subject: Use eval() to execute for loops and if clauses

From: Steven Lord

Date: 4 Oct, 2013 15:54:51

Message: 4 of 5


"Enrico " <enrico.testa@enoi.eu> wrote in message
news:l2mhuj$ebu$1@newscl01ah.mathworks.com...
> Christopher Creutzig <Christopher.Creutzig@mathworks.com> wrote in message
> <524EB7E7.4070708@mathworks.com>...
>> On 04.10.13 12:18, Enrico wrote:
>>
>> > I have a code that writes a string depending on what happened before in
>> > the program. The string created is more MATLAB code possibly containing
>> > for loops or if clauses.
>>
>> I think you should seriously consider solving your problem in a
>> different way. Creating code on they fly is usually not a good idea.
>>
>> > I want to execute the string as if it were MATLAB code, i tried to use
>> > the eval() function but it did not work out.
>> >
>> > A simple example is:
>> >
>> > a=5;
>> > eval('if a>4 b=1 end');
>> >
>> > MATLAB returns the error: Error: Illegal use of reserved keyword "end".
>>
>> Which is exactly what happens if the string were MATLAB code outside of
>> eval, so it did just what you said you wanted:
>>
>> >> a=5;
>> >> if a>4 b=1 end
>> if a>4 b=1 end
>> |
>> Error: Illegal use of reserved keyword "end".
>>
>> Maybe you actually wanted to do something like this instead?
>>
>> >> if a>4, b=1, end
>>
>> b =
>>
>> 1
>>
>>
>> But again, try not to use eval. Even if you ignore the
>> debugging/maintaining problems with it, eval will very likely be slower
>> than other routes to your original problem.
>>
>>
>> HTH,
>>
>> Christopher
>
>
> Thank you very much Christopher, it works!
>
> I know it's a bad habit to use the eval function but I have no other ideas
> to solve my problem.
>
> Very briefly.....
>
> I have to run a specific function that backtests decisions over time
> series. The function receives parameters from the user and produces a
> quantity over these parameters. I then optimize these quantities running a
> grid search. Given a number of user-defined conditions (string) and the
> set of parameters (matrix) I test every possible combination of the
> parameters (using nested loops...I know, curse of dimensionality).
>
> The difficulty is that I do NOT know in advance how many nested loops will
> be needed to run the search (1, 2, 100?), it will depend on the number of
> conditions the user sets. So, what I do know is analyze the string the
> user sets, create ad hoc code, and execute it using eval.
> If you have any better ideas, please share

Use NDGRID.

% Prompt for parameter information
nPoints = input('Please enter the number of points to generate between 0 and
1: ');
nDims = input('Please enter the number of dimensions: ');

% Add error checking here; both nPoints and nDims should be real scalar
nonnegative finite integer values

% Create the grid data using c as a comma-separated list to store the
outputs of NDGRID
c = cell(1, nDims);
[c{:}] = ndgrid(linspace(0, 1, nPoints));

% Let's turn the cell array of matrices into one matrix
M = zeros(numel(c{1}), nDims);

% You could do this using CELLFUN without a loop, but the loop is more
straightforward for an example IMO
for k = 1:nDims
    M(:, k) = reshape(c{k}, [], 1);
end


Now each row of M corresponds to one location in your grid. You can loop
over size(M, 1). To see that you have a grid, try using 5 for the first
prompt, 3 for the second, and use this command to visualize it.

scatter3(M(:, 1), M(:, 2), M(:, 3), [], M)

If you need each coordinate to be a different vector (instead of using
linspace(0, 1, nPoints) for each of them) assemble a cell array and use that
as a comma-separated list in your call to NDGRID:

in = cell(1, 2);
in{1} = 1:5;
in{2} = 1:6;
out = cell(1, 2);
[out{:}] = ndgrid(in{:});
out{1}
out{2}

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Use eval() to execute for loops and if clauses

From: Enrico

Date: 15 Oct, 2013 13:52:07

Message: 5 of 5

Thank you very much for your help!

Enrico

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