LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

parse string (chemical formula)

I've made a simple VI that takes a string value of a chemical formula and puts the element symbol and count into a 2-D array.  As I'm just getting started with this, I'm wondering if I'm doing things the best way.  Bascially I used a while loop and a couple of regexes to split up the elements of the string and then used the build array functions to append to 1D arrays and then make a 2D arrayScreen Shot 2014-07-16 at 9.41.04 PM.png

The front panel looks as such.  What I'm wondering, is there a more efficient way to do this, and is regex a common thing to use in LV or do people generally build up functions using the general substring functions?  As background, I've taken core I and II and am now digging in to some of my actual application.  (planning to take core III later this year or next)

 

Thanks!

 

Dana

Screen Shot 2014-07-16 at 9.41.32 PM.png

0 Kudos
Message 1 of 6
(3,197 Views)

...and sorry about the screenshot.  should've cleaned up the block diagram first, as I just realized it looks like the integer shift register is wired to the loop counter, which isn't actually the case.

0 Kudos
Message 2 of 6
(3,193 Views)

Hi Dana,

if you use "Match Pattern" function instead of "Match Regular Expression" your code, in general,  will be faster.

Can you post your VI (for LV2011) so I can try to run a couple of tests, please?

(I would like to try H2O and CH3COOH)

 

Regards,

Marco

0 Kudos
Message 3 of 6
(3,154 Views)

It didn't look like the match pattern function had the \D and \d patterns to get "non-digit" and "digit", although perhaps I could use [0-9] and [^0-9].

 

It only works now if you actually put the numbers of elements following the symbol.  I need to read in a structure with all the element symbols and their properties (like average and monoisotopic masses), then check each symbol in the string against that to see that it's a real symbol.  Unless I make it case sensitive, I would have to still have the numbers after the element, so as to avoid problems like "OS" vs "Os" or "NaRu" vs "NArU"  I've attached the vi as it stands now, but it obviously is just a start, mostly an exercise to get my head around some concepts.

 

Thanks,

 

Dana

0 Kudos
Message 4 of 6
(3,143 Views)

Here's a sample test using Match Pattern (work only for  elements with single letter names):

 

ChemTest.png

0 Kudos
Message 5 of 6
(3,132 Views)

Dana,

 

To answer your question, yes, many people do use regexes with LabVIEW. I am not a regex expert, partly because I was using LV long before regexes were supported. If you are familiar with regexes, you can exploit the power. I suggest you read the detailed help files on the string functions which support regex to see what limitations might be present.

 

In general manipulation of strings can be tricky and may be slow if the strings are large.

 

Will your format for the input strings be enforced such that things like Na1 will always be there? Can someone enter H2O or HOH for H2O1? What about complex organic molecules or polymers? 

 

Move the String Length function outside the loop. It does not change while the loop is running and does not need to be evaluated on every iteration. If you are using a newer version of LV, consider changing to a for loop with conditional stop. Wire Length to N and stop on offset after match < 0.

 

Generally building an array inside a loop can create memory allocation problems. The solution with strings is not as clear cut as with numeric arrays because the array elements are themselves variable length. Since you always append to the arrays just use an autoindexing tunnel rather than the Build Array and shift register. That may allow the compiler to be smarter about memory allocation.

 

Lynn

0 Kudos
Message 6 of 6
(3,114 Views)