LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Reading data from string, with data in front of the searched substring

Dear All,

 

 

I'm relatively new to LabVIEW and currently trying to extract data from a running text.

I have an excel sheet with a huge amount of measurement conditions, which are written in one cell as a fluent text.

The data extraction itself is already working, however, I am not entirely satisfied with the way I get my data when it is in front of a searched string.

 

An example to make myself more clear:

- When I read the text: "On 20/02 The outside temperature is 20.3°C, which is 2°C colder than today.", then I can easily get the value and the unit of the temperature by searching for "temperature is " with the 'Match Regular Expression Function' and then using the output string 'after match' ("20.3°C, which is 2°C colder than today.") to extract the value 20.3 and the unit "°C" using the "Scan From String Function" with a "search for number" followed by a "search for string".

 

However, when the data is in front of the search string, I have made a solution that I am not sure about if it is a good way to solve the problem.

This can be seen in the snippet below:

 

DiscussionForumExample.png

Screenshot_1.png

 

For this I first search for the substring " speed limit" in the string. Then I take the substring resulting from 'Match Regular Expression Function', which returns the substring BEFORE the search result. In this case the later substring is "There is a 120km/h". Then I invert this, so the unit comes first (but inverted) followed by the inverted number in string format. After that I invert both the unit and the string formatted number back. Then I convert the string formatted number into a real number.

 

As it seems to me, this is a weird solution to an easy problem.

 

Anyone suggestions of how I can take on this problem in a more efficient/better/clearer way?

I need to find a modular solution, so that the unit and value can vary in length and contents without further ado.

A fixed string index to start reading from is therefore not an option.

 

 

Thank you!

 (I'm using LV 2017)

 

 

 

 

0 Kudos
Message 1 of 9
(4,059 Views)

You might be able to use the "Scan From String" function. Search for [text] [number] [string] [text]. However, if you have multiple numbers like in your temperature example you will need to search the remaining string for [number] [string] [text]. 

temp2.png

temp.png

Message 2 of 9
(4,030 Views)

Here is another method I just came up with.  I basically just finds the space before the value.  Then reverse back and use Scan From String to get the values directly.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 3 of 9
(4,027 Views)

Another possibility using regex.

 

With Regex.png

 

Ben64

Message 4 of 9
(4,012 Views)

Hi guys,

 

 

Thank you all for your quick responses. All three answers do what I want to do.

I prefer the regex solution by ben64, since its implementation is small and it's easy readable if you have some Regex knowledge (which I have not, but I had a little look into it and have some basic understanding now). If the numbers/units are behind the lookup string, it's just a matter of changing the format string.

 

@gregoryj : For some reason, the snippets do not work, so I had to reproduce the given solutions. It seems that I am missing something in your solution, since I cannot search for the second number ("speed limit"). It gets stuck in the while loop. Is there some code in another case of the case structure other than 0?

 

@crossrulz:  In the real application I remove the spaces of the input string. It becomes something like "AF46.12HZ,BBIenabled;VV,9600baudSerialSpeed" so the way of looking for a space is no option. 

 

I will implement this later in the afternoon or tomorrow. I will test the solutions further then.

 

Thanks guys!

0 Kudos
Message 5 of 9
(3,985 Views)
0 Kudos
Message 6 of 9
(3,964 Views)

I don't know if any of them are going to work if you take all the spaces out of your string. What kind of information are you storing in a file? Is it an instrument configuration? If so, people often use INI files or XML files to store that kind of info. I've had good luck with MGI's read/write anything and OpenG's variant config library.

0 Kudos
Message 7 of 9
(3,960 Views)

Hi gregoryj

 

I successfully implemented the regex method for all my cases. The vi I made is attached below. This method can handle spaces, but doesn't need them to find the place where the separation is between numbers and letters.

 

This vi I made is useable if the data is upfront, behind the searchstring or when it is the searchstring you are looking for.

 

Usage example:

 

String to be analysed:

  

GH=16MHz, 500ms SwitchTime
23 cycles,Output Enabled, MSI, DAB

I first use another VI to transform this string into a single string with no whitespaces nor line endings nor '=' signs etc. the newline tokens are replaced with ';'.

 

GH16MHz,500msSwitchTime;23cycles,OutputEnabled,MSI,DAB

 

In the lookup cluster array I can input the parameters and the read settings. These are read from a separate excel sheet.

 

Search String: GH

Data Position: Behind

Unit Available: TRUE

Data Type: Number

 

Search String: switchtime

Data Position: Upfront

Unit Available: TRUE

Data Type: Number

 

Search String: switchtime

Data Position: Upfront

Unit Available: NO

Data Type: Number

 

Search String: Output

Data Position: Behind

Unit Available: NO

Data Type: Bool

 

Search String: MSI

Data Position: Lookup

Unit Available: NO

Data Type: Bool

 

Search String: DAB

Data Position: Lookup

Unit Available: NO

Data Type: Bool

 

Then I'm planning to output all the results to an XML file.

One string is one measurement condition string, (so indeed instrument configurations) and there are thousands of these cells that I need to process, all with different values, but mostly the same parameters.

 

I thank you all for your help!

Message 8 of 9
(3,945 Views)

Beware that the vi I uploaded above is not tested that much and is therefore probably full of bugs.

0 Kudos
Message 9 of 9
(3,942 Views)