Is it possible to evaluate a colon notation string without using the eval function?
15 views (last 30 days)
Show older comments
In my pursuit of always trying to eliminate the use of the eval function in my code (whether it makes sense to do this or not), I am looking for a way to turn a string such as mystring = '[1:10]'; into the column vector myvector = [1 2 3 ... 10]; without using the eval function.
mystring could be '[1:10]', '[1:1:10]', '[2:2:140 146:2:260 123435]', or just about anything the user puts in there that will evaluate to a vector at the matlab command prompt.
The usage comes from reading a kind of input spreadsheet which contains a colon notation string for each row into matlab, and having matlab create the corresponding vectors. Currently this is done easily for each row with myvector = eval(mystring);. I can't think of a better way to do it... maybe it doesn't exist, but I'm all ears if anybody has some ideas.
I know there is a way to go the other way on the file exchange (vect2colon.m, by Javier Lopez-Calderon). Realistically, the biggest performance hit comes from reading the spreadsheet into Matlab rather than using the eval function, but I'm still curious about this question.
1 Comment
Accepted Answer
Star Strider
on 30 Jun 2015
If you’re doing simple numeric conversions (the strings are always arrays such as you’ve illustrated), str2num is an option to avoid eval:
a = '[1:10]';
an = str2num(a)
ca = class(an)
an =
1 2 3 4 5 6 7 8 9 10
ca =
double
The ‘ca’ assignment isn’t required for the code, it’s there simply to demonstrate that the result is the double array I assume you want.
5 Comments
Jan
on 2 Jul 2015
@Ryan: Ah, no problem. If the subroutine's name contains the string "protected" nothing can happen. ;-)
But as far as I remember str2num checks the string exhaustively before delivering it to eval, such that a 'system(''format C:'')' will be rejected. Will you dare to test this?
More Answers (1)
Image Analyst
on 29 Jun 2015
You can use scanf():
mystring = '[1:10]';
numbers = sscanf(mystring(2:end-1), '%d:%d') % [1,10]
numbers10 = numbers(1):numbers(2) % [1,2,3,4,5,6,7,8,9,10]
% Now turn the 1:10 into 1:100
% in case you really wanted that and it was not a typo.
numbers100 = linspace(numbers(1), 10*numbers(2), 100)
Funny I never have to look for a way to eliminate the use of eval in my code like you because it never goes in there in the first place. I never use it and it never occurs to me that I would even use it at all. It's not in my mindset. I've never had to use it in any of my programs.
6 Comments
Sean de Wolski
on 2 Jul 2015
MathWorks should sell "I used eval" shot glasses to help with the brain hurt :)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!