0

I have a file with non standard format and i want to read specific parts of it, that consist of real or integer numbers.

str1='N.ELBETI.150.L10';

str2='KARDIA_3.150.L20';

str3='axeloos1.15.75.L20';

str4='florina2.6.3.L2.1';

i just want to store the numbers in the central part and at the end of each string, i.e.:

150 and 10 from str1, 150 and 20 from str2, 15.75 and 20 from str3, 6.3 and 2.1 from str4 i have tried several ways but i cannot. Can you help me ?

2
  • What, specifically, have you tried that isn't working, and in what way isn't it working? Commented Apr 1, 2014 at 13:38
  • sorry, regexp works for str1 but not forstr4 Commented Apr 1, 2014 at 14:03

2 Answers 2

2

An alternative to the already posted, nice answer. Apply the following function to each string.

function [num1, num2] = extract_from_string(s)

num1 = str2num(s(find(s == '.',1,'first')+1:find(s == 'L',1,'first')-2));
num2 = str2num(s(find(s == 'L',1,'first')+1:end));

end
Sign up to request clarification or add additional context in comments.

Comments

1

Use regexp like this:

To obtain the first number:

str1(regexp(str1,'\.\d')+1:regexp(str1,'L\d')-2)

To obtain the last number:

str1(regexp(str1,'L\d')+1:end)

These commands also work for the other strings

8 Comments

in both cases i get an answer: Empty string: 1-by-0
I don't know why you get an empty string. Try the other strings and remember to replace str1 to str2/str3/str4 in every part od the command (the first command has 3 str1
thanks, u r right! can you tell me how to build regexp to get the last three characters ('L10' in str1)
remove the +1: str1(regexp(str1,'L\d'):end)
and how to get the first characters (i.e. 'florina2') in str4 ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.