LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Parsing data from a string to an array

Solved!
Go to solution

I am trying to parse this serial data, which is continuously being added to a string. How can I make this into an array that is appended each time through the loop? The data looks like this:

 

2015/08/20 04:31:39, 0.242, 4.382, 0.219, 22.97, 8.211, 381,


2015/08/20 04:31:43, 0.241, 4.380, 0.220, 22.78, 8.202, 463,


2015/08/20 04:31:48, 0.242, 4.380, 0.219, 23.06, 8.196, 543,

 

I have been trying to use the 'scan from string function' block with my 'format string' as 

 

%f/%f/%f %f:%f:%f, %f, %f, %f, %f, %f, %f, 

0 Kudos
Message 1 of 10
(10,846 Views)
Personally I've never had a lot of luck with long scan strings like that. I think I would split the incoming stream on the date and then turn the remainder of the sample into a 1D array of floats using spreadsheet string to array. You convert the timestamp separately.

If the timestamps are accurate you have 5 sec between samples so you have plenty of time.

Mike...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
Message 2 of 10
(10,828 Views)

I think there are 2 ways

1) (exactly as Mike suggests) Use the "Spreadsheet String To Array" with the format set to "%f" and the delimiter set to a comma. This VI is in the strings pallete 4th row 1st column

 

OR if you need to do processing/conversion before saving

 

2) RegEx.

I used one recently to check incoming data for correctness and it worked really well (wanted to make sure only valid characters for hostnames were allowed to be stored in a hostname value).

 

My regex skills are poor but I found Regular-Expressioin.info helpful

and then to check and make sure the expression was doing what I expected the Regexr site was really good as you can put your own text and expression in and see what matches instantly.

 

Then all you need to do is use the "Match Pattern" VI from the string pallete (2nd row and 3rd column)

You just use the bits of the output from the VI you want as it splits the string into 3 parts and puts the parts on different wires

  1. Before match
  2. match
  3. string after match

You can loop or chain these up as necessary.

1 possible scenario would be to loop for a line of data and put another loop inside that grabs the 7 values based on the ','

 

 

Message 3 of 10
(10,708 Views)

@mikeporter wrote:
Personally I've never had a lot of luck with long scan strings like that. I think I would split the incoming stream on the date and then turn the remainder of the sample into a 1D array of floats using spreadsheet string to array. You convert the timestamp separately.

If the timestamps are accurate you have 5 sec between samples so you have plenty of time.

Mike...

I like this method a lot.  I've even used it to store and retrieve (small) arrays in ini files.  Like maybe an array of parameters or something.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
Message 4 of 10
(10,686 Views)
Solution
Accepted by topic author LabviewChump

@mikeporter wrote:
Personally I've never had a lot of luck with long scan strings like that. I think I would split the incoming stream on the date and then turn the remainder of the sample into a 1D array of floats using spreadsheet string to array. You convert the timestamp separately.

Actually, convert the timestamp first.


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 5 of 10
(10,605 Views)
Yes, excellent!

Mike...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 6 of 10
(10,562 Views)

Your code worked great, thanks for the help. 

 

Quick question: The output array has extra rows of zeros that I want to delete. What's the easiest way to do this?

0 Kudos
Message 7 of 10
(10,550 Views)
There will probably be one extra element. The reason is that the last character in the string is a comma (the delimiter).

Mike...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 8 of 10
(10,542 Views)

jgraf wrote:

Quick question: The output array has extra rows of zeros that I want to delete. What's the easiest way to do this?


I would probably use Clear White Space to get rid of any spaces or end of line character and then String Subset with the length set to the string length-1 to remove the last comma.  If you use the Spreadsheet String To Array after doing that, the extra 0s should go away.


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 9 of 10
(10,538 Views)

This seems to work.

ScanRegExp.png

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 10 of 10
(10,405 Views)