필터 지우기
필터 지우기

How to avoid using eval in this case

조회 수: 1 (최근 30일)
Nico S
Nico S 2016년 1월 16일
댓글: Star Strider 2016년 1월 16일
Hello, im quite new in matlab. i already search a few topics about eval, but i cant find any useful topic for my problem ill just show the simple example, not the whole code
ex:
% code
for subject = 1:22
for vid = 1:40
for i = 1:32
eval(sprintf('s%dmyvariable%d(%d,1) = YYY',subject,vid,i));
end
end
end
and it will generate: s1myvariable1 s1myvariable2 s1myvariable3 ... ... s1myvariable40 s2myvariable1 ... s22myvariable40
the code generates 880(22x40) dynamic variables name.. i use eval, and i have no problem so far except lots of variables in my workspace. But a few topics, well there's a lot of them actually, said that eval is evil, eval is not a friend, dont go near eval, youre gonna hurt yourself. but i cant find another alternatives.. can anyone help me please
  댓글 수: 2
Image Analyst
Image Analyst 2016년 1월 16일
And how would you actually refer to or use those variables later if you don't know their names? Let's say that , for some reason, you wanted to use s13variable37. Would you say x=s13variable37? But what if that variable doesn't exist because subject and vid didn't go that high? You're better off using Star's solution, which is like the vast majority of programmers in any other language (which may not have eval) would do.
Nico S
Nico S 2016년 1월 16일
ill learn about that other solution then, i dont want to use this eval too often, i have lots of eval in my code haha. Thanks

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

채택된 답변

Star Strider
Star Strider 2016년 1월 16일
Might I suggest instead:
MyVariable(subject,vid,i) = YYY;
  댓글 수: 4
Nico S
Nico S 2016년 1월 16일
i think this one can solve my problem, ill try using this one and yes YYY is not a scalar thanks guys
Star Strider
Star Strider 2016년 1월 16일

Great!

Our collective and individual pleasures!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by