Hi,
Given the following string:
[Some text]
type="line" coords="461,461,461,487,461,487,461,489,462,490,462,492,463,493,464,494,465,495,467,495,468,496,470,496,470,496,801,496,801,496,803,496"
[Some text]
type="line" coords="461,487,461,489,462,490,462,492,463,493,496"
How can we obtain the numbers using regexp function of MATLAB? Please note that the length of numbers in the string is variable, therefore, a " universal" expression is needed here.
Thanks in advance,

 Akzeptierte Antwort

Guillaume
Guillaume am 4 Dez. 2015
Bearbeitet: Walter Roberson am 4 Dez. 2015

0 Stimmen

If you want the numbers from all the lines just as a vector and assuming there are no unwanted numbers in any other line then it's simply:
str = sprintf('%s\n%s\n%s\n%s\n', ...
'[Some text]', ...
'type="line" coords="461,461,461,487,461,487,461,489,462,490,462,492,463,493,464,494,465,495,467,495,468,496,470,496,470,496,801,496,801,496,803,496"', ...
'bla bla bla', ...
'type="line" coords="461,487,461,489,462,490,462,492,463,493,496"');
numbers = str2double(regexp(str, '\d+', 'match'))
if you want something more complicated (such as a cell array of vectors one per line or just the number after coords="), then clarify

4 Kommentare

Ive J
Ive J am 4 Dez. 2015
Dear Guillaume ,
Thanks for your answer; however, your assumption is not the case here, because [some text] may contain numeric data, otherwise, it would be easy as you mentioned in your response.
Guillaume
Guillaume am 4 Dez. 2015
So, what do you want? Just the number after any 'coords = "'? Only when the 'type' is 'line'? What are the restrictions?
Guillaume
Guillaume am 4 Dez. 2015
Bearbeitet: Guillaume am 4 Dez. 2015
If the numbers have to be enclosed within the coords quotes, then you need a minimum of two regexp:
coordsegments = regexp(str, '(?<=coords=")[^"]*', 'match');
coords = regexp(coordsegments', '\d+', 'match')
coordvalues = cellfun(@str2double, coords, 'UniformOutput', false)
Ive J
Ive J am 4 Dez. 2015
Yes, that is exactly what I was looking for.
Deeply appreciate your time.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Thorsten
Thorsten am 4 Dez. 2015
Bearbeitet: Thorsten am 4 Dez. 2015

0 Stimmen

line = 'type="line" coords="461,461,461,487,461,487,461,489,462,490,462,492,463,493,464,494,465,495,467,495,468,496,470,496,470,496,801,496,801,496,803,496"'
data = sscanf(strrep(line, 'type="line" coords="', ''), '%d,');
line = 'type="line" coords="461,487,461,489,462,490,462,492,463,493,496"';
data = sscanf(strrep(line, 'type="line" coords="', ''), '%d,');

3 Kommentare

Ive J
Ive J am 4 Dez. 2015
Dear Throsten,
Thanks for your response. However, your code returns an empty answer (assuming "line" in your code contains the given string). Please note that [Some text] in the given string may be problematic in this approach, since %d correspond to "any" number in the string, and that's not the case.
Thorsten
Thorsten am 4 Dez. 2015
Bearbeitet: Thorsten am 4 Dez. 2015
Dear Ive,
I changed the code. If you can process the text line by line, you can extract the data. It works for different number of coords, and ignores all lines that do not start with 'type="line" coords="'.
Ive J
Ive J am 4 Dez. 2015
Bearbeitet: Ive J am 4 Dez. 2015
Unfortunately, it is not possible to distinguish among lines. Nevertheless, I deeply appreciate your time and help.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by