Dynamic Variable (eval=evil ? ) any other solution

5 views (last 30 days)
Hello, I have to dynamically create vectors (variables) with different sizes. They will contain coordinates which will be use to evaluate given functions. I can create the variable names using eval (I have been reading people are saying eval=evil but I can't find other way around) but I could not find a way to use these vectors to evaluate given functions. For example
x=0:0.1:3 ; % Lets assume these are the coordinates
% I need to divide this coordinates into sub-coordinates using indices of x vector. in this example size of x is %(1,31) . Left and Right matrices corresponds to the x indices.
Left=[3;15;25]; Right=[12; 22; 30];
% I do this to create x vectors for each function (now it is 3)
for i=1:1:3
eval(['x_func_' num2str(i) '= x(Left(i):Right(i))']);
end
% This will create x_func_1, x_func_2 and x_func_3 vectors with x values with respect to x;
Now I want to use this sub-coordinates to evaluate functions As in Val_fun_1=sin(x_func_1), Val_fun_2=tan(x_func_2) ....., I tried to use similar eval approach but i did not work.
I have two questions.
  1. if eval is evil. How else I can do this? Because I need to create variables with respect to number of sub-coordinates which will change for every run.
  2. if there is no way out of eval, how I can do the second step. Evaluating wrt to function.
Thank you

Accepted Answer

Star Strider
Star Strider on 23 Nov 2015
Edited: Star Strider on 23 Nov 2015
I would use a cell array. Cell arrays allow for different cells to have different sized data, and even different data types.
For example:
A{1} = randn(1,20);
A{2} = randi(99, 5, 10);
EDIT Following the dictum of ‘never say “never” and never say “always”’, eval is sometimes necessary if you are reading files or processing data created by other programs that created dynamic variables, and want to put those data into a matrix or cell array in your MATLAB scripts. The idea here is to correct the previously-committed errors.
In the instance you cite, however, I would prohibit such dynamic variable creation and use. It would be very bad programming practise.
  2 Comments
Kamuran
Kamuran on 23 Nov 2015
Thank you. I never used cell arrays before. One more thing I learned today :) .

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 23 Nov 2015
Down and down the rabbit hole we go... You started using eval to generate your variables and now in order to use them you need more eval. Soon enough the whole program is just one big eval with no syntax checking, no way of debugging, no optimisation.
I'm not entirely clear on what you're trying to do, but if I understood correctly, you don't need eval at all. You just need to put your subvectors into an appropriate container. A cell array is the simplest one to use.
x = 0:0.1:3;
Left=[3;15;25];
Right=[12; 22; 30];
%see note below
subx = cell(size(Left)); %note that naming a variable xx_fun is misleading unless it contains a function handle
for i = 1:numel(Left)
subx{i} = x(Left(i) : Right(i));
end
%note: the whole code from 'see note below' can be replaced by
subx = arrayfun(@(l,r) x(l:r), Left, Right, 'UniformOutput', false);
To then use your subvectors, you can again use a loop:
funs = {@sin; @tan; @cos}; %function handles to call on respective subvector
%again see note
out = cell(size(subx));
for i = 1:numel(subx)
out{i} = funs{i}(subx{i});
end
%note, the above can be also written as
out = cellfun(@(fn, v) fn(v), funs, subx, 'UniformOutput', false);
  2 Comments
John D'Errico
John D'Errico on 23 Nov 2015
I had to laugh about the rabbit hole. And quite correct too.

Sign in to comment.

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!