MATLAB Answers

Error using eval function

78 views (last 30 days)
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 Comments
HIRAKJYOTI BASUMATARY
HIRAKJYOTI BASUMATARY on 21 Dec 2017
thanx a lot sir. I will definitely keep your words in mind.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 21 Dec 2017
Do not use eval() for that.
words = regexp(collection, '\s+', 'split');
  3 Comments
Walter Roberson
Walter Roberson on 21 Dec 2017
The existing code substituted quote marks for space characters, so I figured the original string must be like 'hero zero horse cow'

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!