string to num

Hi,
I am reading string data of this format from a text file:
(20:200,40:300)
This is the area of an image which I wish to select. How do I convert this string to number range. Thanks.
Charles-

 采纳的回答

Hi Charles,
a simple way would be replace all non digits by spaces and use str2num afterwards:
x = '(20:200,40:300)';
x(~isstrprop(x, 'digit')) = ' ';
xNumber = str2num(x)
Titus

12 个评论

Thanks for your response. But this only extracts all the numbers present.
That's right. But I guess xNumber(1):xNumber(2) and xNumber(3):xNumber(4) are what you are needing, aren't you?
yes, but the numbers could vary, i.e. 4, 47...I have to process large amount of files. Thanks for your suggestions.
Charles, I don't understand. Of course they can vary: take another x (read from your file) and you will get other numbers ...??
Ok, first of all, xNumber = str2num(x) gives a single number. How do you grab the individual elements of the number? xNumber(1) wouldn't return the first number.
Secondly, assuming that it does work and using the above as an example. xNumber(1) returns 2. What if the first number is 20 instead, or 202? Hope you understand.
A note: you can also use RESHAPE to reshape the numbers if there are multiples.
Hi Charles, weird. When I run my code above I get a vector xNumber=[20 200 40 300] ...
me too (2011b)
Wow, unbelievable. Thanks a lot. The error that I made is that instead of x(~isstrprop(x, 'digit')) = ' ';
I typed x(~isstrprop(x, 'digit')) = '';
i.e, no space between the ''. I had to type as the matlab is running on another pc. Answer accepted. Thanks a million.
;-) At least it proved worth doing what I once read from some other poster: please keep on asking and accept only when the question is fully answered...
:-) I truly appreciate your assistance. Really, thanks a lot.
str2double(regexp(x,'\d+','match'))

请先登录,再进行评论。

更多回答(1 个)

osman
osman 2011-12-7

0 个投票

use str2num

2 个评论

That won't work. He'd going to need to parse the string based on '(,:' first
As Sean de Wolski pointed out, it doesn't work.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by