[英]how to get several numbers from a string in matlab
I'm struggling with getting numbers out of several strings: 我正在努力从几个字符串中提取数字:
'L/22P/93'
'P/8P/48L/3'
'1L/63P/751' (this one is: 1, 63, 75, 1)
'PL/18'
'P/30P/5'
'PP'
I want to get all numbers, so I can use them for calculation. 我想获取所有数字,因此可以使用它们进行计算。
I have tried using regexp, but I can only get the first number of each string. 我尝试使用regexp,但是我只能获得每个字符串的第一个数字。
One easy way would be to replace all other characters with spaces, then read the string: 一种简单的方法是将所有其他字符替换为空格,然后读取字符串:
function nums = read_numbers(str)
%// This works only for integer numbers without sign
not_digit = (str < '0') | (str > '9');
str(not_digit) = ' ';
nums = sscanf(str, '%u');
end
As the comment says, the function doesn't take in account signs (+/-), the decimal point or real numbers in scientific notation. 就像评论所说,该函数不考虑科学符号中的符号(+/-),小数点或实数。
After saving the above code in the file read_numbers.m
, you can use it then like in this example: 将上面的代码保存在文件
read_numbers.m
,可以像下面的示例一样使用它:
>> read_numbers('L/22P/93');
22
93
While regular expressions can be intimidating, MATLAB's regex
documentation is fairly comprehensive and should be sufficient to help solve this problem. 尽管正则表达式可能会令人生畏,但MATLAB的
regex
文档相当全面,应足以帮助解决此问题。
As others have commented there are a couple questions here that need to be answered in order to provide a comprehensive answer to your question: 正如其他人所评论的那样,这里有几个问题需要回答,以便为您的问题提供全面的答案:
In the meantime this should return what you've requested using regex
, though it assumes your numbers are unsigned integers and you want a maximum grouping of 2 digits. 同时,这应该返回您使用
regex
请求的内容,尽管它假设您的数字是无符号整数,并且您希望最大2位数字的分组。
teststr = '1L/63P/751';
test = str2double(regexp(teststr, '\d{1,2}', 'match'));
Which returns the following array: 它返回以下数组:
test =
1 63 75 1
I would recommend playing around with an online regex tester to see how your inputs affect the results. 我建议与在线正则表达式测试仪一起玩,看看您的输入如何影响结果。 My favorite is regex101 .
我最喜欢的是regex101 。 It's geared for other languages but the MATLAB syntax is similar enough for the most part.
它适用于其他语言,但是MATLAB语法在大多数情况下足够相似。
Let your data be defined as a cell array of strings: 让您将数据定义为字符串的单元格数组:
s = {'L/22P/93'
'P/8P/48L/3'
'1L/63P/751'
'PL/18'
'P/30P/5'
'PP'};
Then 然后
y = regexp(s, '\d+', 'match'); %// cell array of cell arrays of strings
y = cellfun(@str2double, y, 'uniformoutput', 0); %// convert to cell array of vectors
gives the result as a cell array of vectors: 给出结果作为向量的单元格数组:
y{1} =
22 93
y{2} =
8 48 3
y{3} =
1 63 751
y{4} =
18
y{5} =
30 5
y{6} =
[]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.