Is it possible to evaluate a colon notation string without using the eval function?

8 ビュー (過去 30 日間)
Ryan
Ryan 2015 年 6 月 29 日
編集済み: Stephen23 2019 年 1 月 22 日
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 件のコメント
Stephen23
Stephen23 2018 年 9 月 26 日
編集済み: Stephen23 2019 年 1 月 22 日
This is easy to achieve efficiently without eval (either explicitly called or inside str2num). See my answer to this question:

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2015 年 6 月 30 日
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 件のコメント
Ryan
Ryan 2015 年 6 月 30 日
@Jan: I checked and str2num does indeed call eval within a subfunction called protected_conversion :( ... bummer.
Jan
Jan 2015 年 7 月 2 日
@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?

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2015 年 6 月 29 日
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 件のコメント
Ryan
Ryan 2015 年 6 月 30 日
Yes Star's solution works fine... it's just that as Jan pointed out, the str2num function ultimately calls the eval function to do it's business, so it would not be any more efficient than just using the eval function in the first place. Sean & Stephen, I get your point. The need for this arises solely out of user convenience and a long history of doing it this way. It is of course possible to leave numbers as numbers, but the matlab colon notation is a convenient shorthand to creating a list of integers that might happen at regular intervals, skip a group, then continue in a similar patter (such as '[2:2:120 126:2:130]'). This sort of thing comes into play when removing outliers from a dataset, or similar kind of data selection. Ultimately, I think I will just leave the code as is even though it hurts my brain to see eval functions laying around in the code.
Sean de Wolski
Sean de Wolski 2015 年 7 月 2 日
MathWorks should sell "I used eval" shot glasses to help with the brain hurt :)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeVariables についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by