Is it possible to evaluate a colon notation string without using the eval function?
Ältere Kommentare anzeigen
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 Kommentar
This is easy to achieve efficiently without eval (either explicitly called or inside str2num). See my answer to this question:
Akzeptierte Antwort
Weitere Antworten (1)
Image Analyst
am 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 Kommentare
Ryan
am 30 Jun. 2015
"...this might be one exception for which the eval might be suitable..."
This is begging the question: "when executable strings encode numbers, executing them is the best way to get the numbers". But why does this need arise in the first place? When numeric data is encoded in the form of executable strings then of course some slow and non-robust parsing is going to be required... but most likely there would be a much more robust and probably conceptually neater way of doing this anyway, without using those strings at all.
Image Analyst
am 30 Jun. 2015
Ryan, I tested Star's solution with your '[2:2:140 146:2:260 123435]' and it works fine, so I'm not seeing how eval() would be a preferable way to accomplish this task. For the cases you gave, this is not an exception like you suggested.
Sean de Wolski
am 30 Jun. 2015
Agree with Stephen, why are you getting strings like this? Is it user input? If so, rather than an executable string how about three inputs: Lower_end, stride, upper end?
Ryan
am 30 Jun. 2015
Sean de Wolski
am 2 Jul. 2015
MathWorks should sell "I used eval" shot glasses to help with the brain hurt :)
Kategorien
Mehr zu Variables finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!