Find and convert negative number strings

I need to find a negative number between terms A and B in a string. The following code find the 1x5 char '-20', but not as number -20. So I can not use it directly to for continue calculation. What is the easiest way to handle this?
cell2mat(regexp(SDstring,'(?<=Reference Level\d*).*?(?= dBm)','match'));

8 Comments

What does SDstring look like? Can you share it?
"The following code find the 1x5 char '-20'..."
What you show is a 1x3 char, not 1x5.
Stephen23
Stephen23 on 23 Mar 2018
Edited: Stephen23 on 23 Mar 2018
Ivy Chen's "Answer" moved here:
Yes, it is basically to locate the number between Reference Number -50 fieldB. -50 is the term I am looking for. the code only find "-50" char, but not number.
Have tried several ways other folks suggested in forum based on their experiences, but it does not work specifically to what I prefer at this point.
Stephen23
Stephen23 on 23 Mar 2018
Edited: Stephen23 on 23 Mar 2018
@Ivy Chen: please use the comments for commenting. The Answers are for answering the question.
Ivy Chen's "Answer" moved here:
Have tried that as well, it returns "NaN". I assume it does not recognize the "-". I check the ascii on that, it is 45, which the the "-".
Stephen23
Stephen23 on 23 Mar 2018
Edited: Stephen23 on 23 Mar 2018
@Ivy Chen: please use the comments for commenting. The Answers are for answering the question.
See my answer for one way to handle the - sign.
There are two problems:
  • cell2mat which is never going to convert a string of numbers into an actual number. Stephen's answer of using str2double for that is correct
  • the correct regular expression to detect the number. To design the correct regular expression we need to know exactly what is allowed and what isn't. Is the number always an integer or can it be decimal. Is exponential notation (e.g 1.2e5) a possibility? Is the decimal separator always '.'? Can the number include thousands separator ,? etc.
Thanks and here are the specifics. The desired number is always between the 2 terms "Reference Level" AND "dBm" and
  1. It will have space(s) after/before the terms
  2. The number can be decimal
  3. No exponential notation
  4. The decimal separator is always '.'
  5. No thousands separator

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 23 Mar 2018
Edited: Stephen23 on 23 Mar 2018
>> str = 'Reference Level -50 fieldB';
>> num = str2double(regexp(str,'[+-]?\d+','match'))
num = -50

6 Comments

the string is a long ~600 characters, and we are looking for the number specifically between "Reference Number (space)(space) and fieldB'.
I think it captured two spaces before the -50, that is why it is 5 characters, instead of 3 characters.
The regular expression in Stephen's answer will never capture any space. Just an optional sign followed by numeric digits.
>> fmt = '(?<=Reference Level\s*)[+-]?\d+\.?\d*(?=\s*fieldB)';
>> str = 'Reference Level -50 fieldB';
>> str2double(regexp(str,fmt,'match'))
ans = -50
>> str = 'Reference Level -67.8 fieldB';
>> str2double(regexp(str,fmt,'match'))
ans = -67.800
A faster but less flexible alternative would be to use sscanf:
>> str = 'Reference Level -50 fieldB';
>> sscanf(str,'Reference Level%ffieldB')
ans = -50
>> str = 'Reference Level -67.8 fieldB';
>> sscanf(str,'Reference Level%ffieldB')
ans = -67.800
If the number can be decimal, then
fmt = '(?<=Reference Level\s*)[+-]?\d*\.?\d+(?=\s*fieldB)'
Got it. I only change the \s* to \W* to catch additional non-word items between two terms. thanks!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 23 Mar 2018

Edited:

on 23 Mar 2018

Community Treasure Hunt

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

Start Hunting!