필터 지우기
필터 지우기

Dynamic Variable (eval=evil ? ) any other solution

조회 수: 7 (최근 30일)
Kamuran
Kamuran 2015년 11월 23일
댓글: Stephen23 2016년 2월 23일
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

채택된 답변

Star Strider
Star Strider 2015년 11월 23일
편집: Star Strider 2015년 11월 23일
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
Kamuran
Kamuran 2015년 11월 23일
Thank you. I never used cell arrays before. One more thing I learned today :) .
Star Strider
Star Strider 2015년 11월 23일
My pleasure.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Guillaume
Guillaume 2015년 11월 23일
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
John D'Errico
John D'Errico 2015년 11월 23일
I had to laugh about the rabbit hole. And quite correct too.
Stephen23
Stephen23 2016년 2월 23일
+1 made me laugh too!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by