"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 userdefined 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 commaseparated 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 commaseparated 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
