How exactly does eval work?

4 Ansichten (letzte 30 Tage)
Chris
Chris am 5 Jun. 2014
Kommentiert: Geoff Hayes am 5 Jun. 2014
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

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 5 Jun. 2014
Bearbeitet: Geoff Hayes am 5 Jun. 2014
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 Kommentare
Chris
Chris am 5 Jun. 2014
That's it, simple fix! Thank you!
Geoff Hayes
Geoff Hayes am 5 Jun. 2014
You're welcome!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming Utilities finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by