When using the eval function, MATLAB doesn't recognize " , " as an ASCII character, rendering it useless

15 views (last 30 days)
I was trying to implement a code with the following section in it:
for j=1:1:24
eval(['DataAGC(index,',j+1,')=DataAGC(index,',j+1,')+data.AGC.x',j-1,'(i);'])
end
However this dialog came out of it
Error: Invalid text character. Check for unsupported symbol, invisible character, or
pasting of non-ASCII characters.
Then, by trying a much simpler
eval(['x', 1,'=1;'])
The same error pops up
I've implemented a different code but it still worries me for other situations that may come in the future. This happened on the 2018a version, but everything is code that has worked before or should work in my eyes.
I appreciate any help
-JD
  2 Comments
Stephen23
Stephen23 on 9 Sep 2019
Quick comment, the eval function works perfectly when there are commas used:
>> str = ['disp(''','here,','are,','some,','commas,',''')'];
>> eval(str)
here,are,some,commas,

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 10 Jun 2019
Edited: Stephen23 on 11 Jun 2019
Evil eval strikes again!
This is a very good example of how using eval results in slow, complex code that is buggy and hard to debug. Read this to know more about some of the problems that eval causes:
"...everything is code that has worked before or should work in my eyes."
You might think that it "should work", but the obfuscation of eval makes it easy to write code where you have no idea what is actually going on, or how to debug it, or how to fix it.
Totally superfluous eval caused you to write a bug in your code and also makes it hard to figure out what the bug is**. The best solution is of course to get rid of pointless and counter-productive eval entirely, which is of course simple to do using dynamic fieldnames (if you are using a structure, which is not clear in your question as you do not give any details of the data classes):
For example something like this inside the loop (note: NO eval):
fnm = sprintf('x%d',j-1);
DataAGC(index,j+1) = DataAGC(index,j+1) + data.AGC.(fnm)(i);
This makes your code simpler, faster, easier to debug, and much more efficient. Note that putting meta-data into the x### fieldnames (i.e. the pseudo-index) is fundementally not very efficient, and would be better and simpler coded using a non-scalar structure, or any other array type that uses indexing.
** The bug has nothing to do with commas. It is because of the implicit conversion of numeric values to characters, and the characters that that implicit conversion will produce are not valid in variable names or fieldnames. This is easy to identify, once you start looking:
>> +['x', 1,'=1;']
ans =
120 1 61 49 59
% ^ what character do you think this is? (hint: ASCII "Start of Heading")
  2 Comments
Juan Diego Pico
Juan Diego Pico on 10 Jun 2019
You see, the reason I thought of using eval in this case is because I'm reading from a table and the variables are named dynamically like so, which would make it easier to read using the eval function. Thanks for the alternative code.
Stephen23
Stephen23 on 10 Jun 2019
Edited: Stephen23 on 11 Jun 2019
"...which would make it easier to read using the eval function"
In reality it makes code inefficient and hard to debug. You can easily access variables in a table using the basic syntaxes shown in the documentation (none of them require eval):
For example most likely you could simply do something like this:
... + data.AGC{i,fnm}
which is actually easier to read than this (buggy and inefficient) code:
eval(['....+data.AGC.x',j-1,'(i);'])
Identify a suitable syntax for getting data from a table by reading the documentation, or upload your data in a .mat file so that we can help.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!