필터 지우기
필터 지우기

How exactly does eval work?

조회 수: 3 (최근 30일)
Chris
Chris 2014년 6월 5일
댓글: Geoff Hayes 2014년 6월 5일
Hi all,
I did it, even though others warned against using eval, I went ahead and did it!
My use of eval worked just fine in a previous version of a code I have been working on, but in the newest version I must repeat my use of eval, and when I add the second iteration, Matlab spits back an error message (Subscript indices must either be real positive integers or logicals.) that does not make sense to me. Here is the little block of code that uses eval:
rec4 = [rec3 'D'];
rec4 = eval(rec4);
size = size(rec4);
for i = 1:size(2)
fprintf(fid,'%.14f',rec4(i));
fprintf(fid,',');
end
The error message comes from the 'size = size(rec4)' line of code. I am confused though because rec4 is a 1X169 array of real numbers, and when i do a 'whos rec4' I get the following, which makes me believe I should not be receiving the error message:
Name Size Bytes Class Attributes rec4 1x169 1352 double
Again, when I use this block of code the first time in the script, everything works fine, I have been using it for about a month or so now.
Any help would be greatly appreciated, thank you!
-Chris

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 6월 5일
편집: Geoff Hayes 2014년 6월 5일
What is rec3 defined to be? Is the above block within a loop of some kind?
I noticed that you defined a local variable with the name of a in-built MATLAB function:
size = size(rec4);
If this happened on a previous iteration of your loop, then this is probably the cause of the error. For example,
>> rec4 = zeros(1,169);
>> size = size(rec4);
>> size = size(rec4);
Subscript indices must either be real positive integers or logicals.
So the second size fails as it should since it has been redefined as a local variable. Please rename your local variable to something more like rec4Size.
  댓글 수: 2
Chris
Chris 2014년 6월 5일
That's it, simple fix! Thank you!
Geoff Hayes
Geoff Hayes 2014년 6월 5일
You're welcome!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming Utilities에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by