函数在matlab中拆分字符串并返回第二个数字

时间:2021-10-07 17:54:37

I have a string and I need two characters to be returned. I tried with strsplit but the delimiter must be a string and I don't have any delimiters in my string. Instead, I always want to get the second number in my string. The number is always 2 digits.

我有一个字符串,我需要返回两个字符。我尝试使用strsplit,但分隔符必须是一个字符串,我的字符串中没有任何分隔符。相反,我总是希望在我的字符串中得到第二个数字。该数字始终为2位数。

Example: 001a02.jpg I use the fileparts function to delete the extension of the image (jpg), so I get this string: 001a02 The expected return value is 02

示例:001a02.jpg我使用fileparts函数删除图像的扩展名(jpg),所以我得到这个字符串:001a02预期的返回值是02

Another example: 001A43a . Return values: 43

另一个例子:001A43a。返回值:43

Another one: 002A12. Return values: 12

另一个:002A12。返回值:12

All the filenames are in a matrix 1002x1. Maybe I can use textscan but in the second example, it gives "43a" as a result.

所有文件名都在矩阵1002x1中。也许我可以使用文本扫描,但在第二个例子中,它给出了“43a”。

2 个解决方案

#1


(Just so this question doesn't remain unanswered, here's a possible approach: )

(就这样,这个问题没有得到解答,这是一种可能的方法:)

One way to go about this uses splitting with regular expressions (MATLAB's strsplit which you mentioned):

解决这个问题的一种方法是使用正则表达式拆分(你提到的MATLAB的strsplit):

str = '001a02.jpg';
C = strsplit(str,'[a-zA-Z.]','DelimiterType','RegularExpression');

Results in:

C = 
    '001'    '02'    ''

In older versions of MATLAB, before strsplit was introduced, similar functionality was achieved using regexp(...,'split').

在旧版本的MATLAB中,在引入strsplit之前,使用regexp(...,'split')实现了类似的功能。

If you want to learn more about regular expressions (abbreviated as "regex" or "regexp"), there are many online resources (JGI..)

如果您想了解有关正则表达式(缩写为“regex”或“regexp”)的更多信息,那么有许多在线资源(JGI ..)

In your case, if you only need to take the 5th and 6th characters from the string you could use:

在您的情况下,如果您只需要从字符串中取第5和第6个字符,您可以使用:

D = str(5:6);

... and if you want to convert those into numbers you could use:

...如果你想将它们转换为数字,你可以使用:

E = str2double(str(5:6));

#2


If your number is always at a certain position in the string, you can simply index this position.

如果您的数字始终位于字符串中的某个位置,则可以简单地索引此位置。

In the examples you gave, the number is always the 5th and 6th characters in the string.

在您给出的示例中,数字始终是字符串中的第5个和第6个字符。

filename = '002A12';
num = str2num(filename(5:6));

Otherwise, if the formating is more complex, you may want to use a regular expression. There is a similar question matlab - extracting numbers from (odd) string. Modifying the code found there you can do the following

否则,如果格式化更复杂,您可能需要使用正则表达式。 matlab有一个类似的问题 - 从(奇数)字符串中提取数字。修改在那里找到的代码,您可以执行以下操作

all_num = regexp(filename, '\d+', 'match'); %Find all numbers in the filename 
num = str2num(all_num{2}) %Convert second number from str

#1


(Just so this question doesn't remain unanswered, here's a possible approach: )

(就这样,这个问题没有得到解答,这是一种可能的方法:)

One way to go about this uses splitting with regular expressions (MATLAB's strsplit which you mentioned):

解决这个问题的一种方法是使用正则表达式拆分(你提到的MATLAB的strsplit):

str = '001a02.jpg';
C = strsplit(str,'[a-zA-Z.]','DelimiterType','RegularExpression');

Results in:

C = 
    '001'    '02'    ''

In older versions of MATLAB, before strsplit was introduced, similar functionality was achieved using regexp(...,'split').

在旧版本的MATLAB中,在引入strsplit之前,使用regexp(...,'split')实现了类似的功能。

If you want to learn more about regular expressions (abbreviated as "regex" or "regexp"), there are many online resources (JGI..)

如果您想了解有关正则表达式(缩写为“regex”或“regexp”)的更多信息,那么有许多在线资源(JGI ..)

In your case, if you only need to take the 5th and 6th characters from the string you could use:

在您的情况下,如果您只需要从字符串中取第5和第6个字符,您可以使用:

D = str(5:6);

... and if you want to convert those into numbers you could use:

...如果你想将它们转换为数字,你可以使用:

E = str2double(str(5:6));

#2


If your number is always at a certain position in the string, you can simply index this position.

如果您的数字始终位于字符串中的某个位置,则可以简单地索引此位置。

In the examples you gave, the number is always the 5th and 6th characters in the string.

在您给出的示例中,数字始终是字符串中的第5个和第6个字符。

filename = '002A12';
num = str2num(filename(5:6));

Otherwise, if the formating is more complex, you may want to use a regular expression. There is a similar question matlab - extracting numbers from (odd) string. Modifying the code found there you can do the following

否则,如果格式化更复杂,您可能需要使用正则表达式。 matlab有一个类似的问题 - 从(奇数)字符串中提取数字。修改在那里找到的代码,您可以执行以下操作

all_num = regexp(filename, '\d+', 'match'); %Find all numbers in the filename 
num = str2num(all_num{2}) %Convert second number from str