How exactly does eval work?
9 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!