LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to change 1D ARRAY to singles single string elements

I'm using TCP Read to receive messages from my client.


The client sends a block of 100 lines, each containing 3 float values, encoded in binary (not as plain text), in the following format:

[%f, %f, %f
%f, %f, %f
...
] (but binary)


TCP Read returns a string. I’m not sure whether this function automatically converts the binary data into a string, but it’s this string that I want to split line by line, and then extract each value to access every individual element.

0 Kudos
Message 11 of 16
(180 Views)

@Ouinon wrote:

Hello,

I used "Spreadsheet String to Array" to separate my incoming data into individual lines. Now I have something like this:

["%f, %f, %f", "%f, %f, %f", "%f, %f, %f", ...]   Each string contains three comma-separated values.

 

What I would like to do is extract the three values from each string and place them into three separate float arrays.

For example, given:

["1,2,3", "4,5,6"]

 

I want to obtain:

Array 1: [1.0, 4.0]
Array 2: [2.0, 5.0]
Array 3: [3.0, 6.0]

 

How can I do this efficiently in LabVIEW?

Thanks in advance!


 

First you hijack an old thread titled "How to change 1D ARRAY to singles single string elements" which has no resemblance on what you are trying to do, then you post a very vague and ambiguous questions and are surprised that it won't work.

 

In order to actually help, you need to attach* a simple VI containing a typical input string, e.g. as diagram constant and an explanation what you are trying to get from it in the end.

 

You have an input string, right?

<all intermediary steps are irrelevant. We can figure out the correct way>

You apparently want N 1D arrays as output (wouldn't a single 2D array be more reasonable?).

 

Is the number of rows and the number of elements per row known from the beginning or can it change?

 

I recommend not to use words like "most efficient" because that loaded word has many different meaning for different people (speed? memory? less code? scalability? debuggability?, etc.). The first step is to get the correct result, and we are obviously not there yet. We can worry about efficiency later. 😄

 

*If you have a program that generates that string, add an indicator to that wire, then run the VI so the indicator shows a typical string. Now right-click that terminal and "create constant". Copy that constant to a new VI and save that VI under a new name. Use "save for previous", 2019 or below, so everybody here can open it. Then explain exactly what you wan to get out of it (three arrays of strings? three numeric arrays?, something else?)

 

Once you do that, we'll have a look!

0 Kudos
Message 12 of 16
(179 Views)

You still did not attach example data. That would make it immediatley obvious that your approach cannot work. If you have binary data, spreadsheet string to array will not help you. 

 

Again: use probes! Right-click wire  -> Probe. Then you will see that TCP Read does not convert data to strings.

 

A lot of function like TCP Read or VISA Read return strings, even if the protocol is not ASCII based. Use unflatten from string to turn the "string" into the proper data type.

 

unflatten from stringunflatten from string

0 Kudos
Message 13 of 16
(162 Views)

@Ouinon wrote:

I'm using TCP Read to receive messages from my client.


The client sends a block of 100 lines, each containing 3 float values, encoded in binary (not as plain text), in the following format:

[%f, %f, %f
%f, %f, %f
...
] (but binary)


TCP Read returns a string. I’m not sure whether this function automatically converts the binary data into a string, but it’s this string that I want to split line by line, and then extract each value to access every individual element.


Everything (everything!!!) in a computer is "binary" one way or another! The word binary makes no sense here (unless with "binary" you mean formatted in binary using characters "0" and "1") because a binary string has no concept of "lines" (that's a construct for readable strings, indicated by the presence of a special linefeed character. You cannot have both! (if a DBL is cast into a binary string, there is a nonzero chance that a linefeed can occur by accident and that would break everything!

 

Do you have documents on the client software that sends the string? Is it LabVIEW? Why wrote it? Can you show that code too?

 

If the binary string represents a 2D array of "floats" (DBL? SGL? Root-beer?), chances are that the first eight bytes contain dimension information but that is not guaranteed either.

0 Kudos
Message 14 of 16
(151 Views)

Hello,

Thank you to everyone who helped me with this issue. I was unclear about the type of data to send because I wasn't sure how I was receiving it myself.


I discovered the existence of probes — incredible tools (thanks Cordm!).

 

What I receive is a block of 300 doubles in raw data form (every 10 ms):

What I send:  buffer[300] = {0.001, -0.987, -0.456, 0.403, 0.631, -0.765, -0.654, -0.894, 0.002, ... }

What I receive:

Ouinon_1-1749192820809.png

 

 

I then used Cordm's code with the "Unflatten from String" block, and afterwards, I used the "Decimate 1D Array" block to split my array of doubles into 3 parts.

 

Thanks to all of you, I was able to solve the question I posted on this thread.
Sorry for reopening it — I thought it was related to the original topic, but in the end, it wasn’t really.

 

Thanks again to everyone who helped. Here's the final code:

 

Ouinon_0-1749192607940.png

 

 

The binary string is the result of a TCP Read. (I'm not showing the rest of the code because it's not relevant to the question, but I can share it if needed.)

0 Kudos
Message 15 of 16
(105 Views)

Good, you figured it out.

 


@Ouinon wrote:
  1. What I receive is a block of 300 doubles in raw data form (every 10 ms):
  2. What I send:  buffer[300] = {0.001, -0.987, -0.456, 0.403, 0.631, -0.765, -0.654, -0.894, 0.002, ... }
  3. What I receive:...

 

I am still confused about the steps. It was never clear how you send that array in step 2, but that should fully determine what do do on the receiving end in step 3. It is not clear how step 1 fits into the grand scheme of things.. 😄

0 Kudos
Message 16 of 16
(92 Views)