data extract
1 次查看(过去 30 天)
显示 更早的评论
I have data in a single column in the following format:
123456-123456.123.abcde
I would like to extract 123456 between - and .
0 个评论
采纳的回答
Fangjun Jiang
2011-11-9
str='123456-123456.123.abcde';
num=regexp(str,'-[^\.]*','match');
num=str2double(num{1}(2:end))
Update
a=dir('*.bin');
b={a.name};
c=regexp(b,'-[^\.]*','match');
d=-cellfun(@str2double,c)
d =
200000 200001 200002
6 个评论
Fangjun Jiang
2011-11-9
By the way, when I say valid data in MATLAB, I mean you write down something in your question so others can copy and paste to test it in the code. The three lines in your comment are not really valid data in MATLAB. You could provide it as str={'123456-200000.123.bin';'123456-200001.153.bin';'123456-200002.126.bin'}. So when others copy it, they have the data right away in MATLAB to work with.
更多回答(1 个)
Walter Roberson
2011-11-9
t = regexp(str, '-(\d+)', 'tokens');
str2double(t{1}{1})
4 个评论
Walter Roberson
2011-11-9
It is not very different from Fangjun's version, but involves fewer operations. regexp looks through each of the input strings, looking for a pattern of interest. The pattern of interest starts with a "-" and ends just before the first non-digit after that. The () indicate that whatever pattern inside the () is matched is to be recorded separately, so since the pattern is "one or more digits", those digits are recorded separately (i.e., without the leading "-" that was part of the matching pattern.) The 'tokens' parameter says to return the parts that were recorded separately (the "tokens" that the pattern marked as being of interest.)
The list of tokens is returned all in one cell array, and inside the cell array is a list of cell arrays, one per input string; inside there is the character array. The cellfun iterates over all of individual outputs (one per input line) and unwraps a cell array level from what is there and converts the result from text to a double precision number.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!