Error using eval function
7 次查看(过去 30 天)
显示 更早的评论
I am trying simple eval function . But at some times it gets executed fine. Sometimes it gives me the error msg "Error: Unexpected MATLAB expression". Here's the eval function code. I hope i got the syntax correctly. collection is a character array = 'hero','zero','horse','cow'
collection = regexprep(collection,' ',''',''');
eval(['words = {''',collection,'''};']);
2 个评论
Stephen23
2017-12-21
编辑:Stephen23
2021-12-4
Lets have a look at your question in detail, because it is a good example of why using eval is a poor way to write code:
"I am trying simple eval function."
That is the bad design decision right here.
"But at some times it gets executed fine. Sometimes it gives me the error msg "Error: Unexpected MATLAB expression"."
Notice how the message is not very helpful. Because you used eval it cannot show you the line or particular code position where the error occurs. By using eval you just made debugging much more difficult.
"Here's the eval function code. I hope i got the syntax correctly."
If you had written your code normally without eval then the MATLAB editor would highlight any syntax errors and even suggest how to fix them. And of course the editor also offers tab completion, variable highlighting, etc. etc. When you decided to use eval none of these features work, so you just picked a way of writing code that make it harder for you to write good code. And harder to debug it!
Summary: you picked a way of writing code that is slow, buggy, hard to debug, and obfuscates the code intent. You then found your code to be buggy and hard to debug. Even though what you are attempting is actually trivially simple because you used eval it was too difficult for you to fix by yourself, and you had to ask random strangers to help you.
All of this could have been avoided very simply: by not using eval. Read this to know some of the reasons why:
采纳的回答
Walter Roberson
2017-12-21
Do not use eval() for that.
words = regexp(collection, '\s+', 'split');
3 个评论
Stephen23
2017-12-21
编辑:Stephen23
2017-12-21
"But sir how should i get only the words(strings) under different cells"
regexp returns a cell array with as many cells as required. You can use basic cell indexing to access the cell contents.
"collection is a character array = 'hero','zero','horse','cow'"
There seems to be some confusion about what array type you are using. What you show in your question looks like a cell array of character vectors (and not a character array itself), in which case it is not clear what you are trying to achieve.
If you really do have a character array as your wrote, e.g.
collection = ['hero','zero','horse','cow']
then this is actually equivalent to
collection = 'herozerohorsecow'
because [] are a concatenation operator in MATLAB. Note that there is no general solution to splitting any arbitrary concatenated words (except in some cases when you know something about those words).
Walter Roberson
2017-12-21
The existing code substituted quote marks for space characters, so I figured the original string must be like 'hero zero horse cow'
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!