LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

string conversion to proper type based on string content

Solved!
Go to solution

Hi,

I have a csv file with some string values, and I want to assign them to to proper type in labview, to then assing them to some pre-exisiting teststand properties.

I'm thinking one particular way of sensing a number string should be in labview somewhere, but I can't seem to find it.  There is certainly decimal string to number, and fractional string to number, but neither return an error when passing them a string like "blah".  They just return 0. 

 

So my main challenge here is to sense a number string, either with single decimal point or not, and sense that it is a "number".

 

Example:

String -> evaluates to

 

1.234 -> is number

1234 -> is number

192.168.1.12 -> is not a number

 

I could write a vi to do this, but I was wondering if there wasn't an already obvious tool in labview, which is eluding me right now.  I don't like re-inventing the wheel even for something so simple.

 

Thanks

David J.

 

0 Kudos
Message 1 of 3
(2,820 Views)

Well, if there is such a tool, I don't know about it.  I did exactly that with an Excel Worksheet.  I had a Worksheet of maybe 100 columns and multiple rows.  I wanted to create an Excel Cluster whose element Names were taken from the first (Header) row and whose data type was "deduced" from the contents of the first data row.  To make it more complicated, a few of the entries represented (X, Y) pairs of points (they were spatial coordinates for laser targets) and I wanted to write this as 5, 20 rather than have two columns (5 and 20).

 

So I wrote a mini-parser.  Just a sec while I look up the rules that I used.  OK, recall I'm reading in data from an Excel Worksheet, which I read in as a string.  Here are my rules and the order they are applied:

   String consists of spaces (i.e. there is no "There" there) -- Unknown Type, exit.

   String consists of <number>, <number> where <number> can have leading spaces, sign, numerals, decimal point, more numerals.  As it happens, "," will match.  I used the pattern " *[\+-]?\d*\.?\d*, *[\+-]?\d*\.?\d* *".  Match means my cluster type.
   String is <number><spaces>, where decimal point is required ("." will match).  " *[\+-\?\d*\.\d* *".  Match means Dbl (I don't use Sgl in my LabVIEW code).

   String is <number><spaces> without decimal point, with at least one numeric digit required.  " *[\+-]?\d+ *".  Match means I32 (I don't distinguish between I32 and other Integer types).

   Anything else is String (I don't use Booleans, otherwise I could search for True or False).

 

Bob Schor

 

 

 

0 Kudos
Message 2 of 3
(2,774 Views)
Solution
Accepted by topic author david_jenkinson

@david_jenkinson wrote:

 

So my main challenge here is to sense a number string, either with single decimal point or not, and sense that it is a "number".

 

Example:

String -> evaluates to

 

1.234 -> is number

1234 -> is number

192.168.1.12 -> is not a number

 


In the more general case, you would also need to be able to correctly parse e.g,  -10e6, 85.83E-3 and such as proper floating point number. Also your last example is basically a U32 number, just formatted in IP address style.

 

You should use "scan from string" with  %f as format and NaN as default. Then also check if the remaining string is an empty string. If it is not, then it is not a number according to your narrow definition.

 

 

 

Message 3 of 3
(2,744 Views)