From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Send array from MatLab to Labview via UDP

Solved!
Go to solution

Hello.

I am trying to send an array of values from MatLab to Labview via UDP.

 

I understand MatLab writes to UDP in binary if one uses the "fwrite" command and I can use a simple string to byte array conversion for that, but I want to retain the numerical value of each entry in the array.

 

If I send the array data via "fprintf" I can send the exact values of the array, but as a string.

 

I have been struggling with extracting the data from the string and reassembling it into the array I desire. I found an example that seemed to be something similar to what I am doing. It used the "Scan from String" function, but I am receiving an error when I run it (Error 85: Scan failed. The input string does not contain data in the expected format). I see the correct values being read in, but then it experiences the error after all 3 values have been read. I've attached a screenshot of the VI I'm trying to build. 

 

I would like to be able to receive a 1x3 matrix in this script (and a 1x6 matrix in another script).

0 Kudos
Message 1 of 10
(3,895 Views)

We cannot work with pictures*, so please attach the VI. How does the received string look like?

How does matlab format/cast the array to a string? Can you show that code too?

 

* there are many things wrong with your code, even if we can't see what's in the other cases, etc.:

  1. no visible toplevel loop. How are you running this?
  2. mismatched placement of UDP ope/UDP close, i,e, on different diagram layers).
  3. Overuse of local variables.
  4. Blatant coercion dots everywhere.
  5. toplevel breakpoint set (red frame)
  6. Don't talk about "matrix", all you'll should get is 1D arrays.
  7. etc.etc
  8.  
0 Kudos
Message 2 of 10
(3,867 Views)

Thanks for the response.

I've attached the VI to this post and a picture of probe results from the string containing the 1x3 array being read in from MatLab. 

 

As far as the VI itself, it's a controller for a wheelchair. I'm trying to read in the values from classifiers (from MatLab) that will be fed into a fuzzy logic controller. The classifier values are 5 digit (0.xxxx) precision numbers, but when MatLab writes them into LabView they are displayed as 8 digit  precision numbers in scientific notation (x.xxxxxxxe-01). The controller directs a header to a certain direction. This is a sample VI that mimics what the actual one will be.

 

Previously, I was writing in an array of binary data and I could accomplish that with a string to byte array conversion. However, I need the extra precision for more accurate control with the fuzzy logic controller.

 

I apologize for my newness...

Download All
0 Kudos
Message 3 of 10
(3,860 Views)

I should think that a double would be much more accurate sent as binary bytes rather than a string of ASCII characters representing a number accurate to 5 places because the 8 bytes sent is the double, itself.

 

Don't do any kind of conversion before sending.  Send the actual value.

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
(3,854 Views)

It is to my understanding that double precision is the default format for matlab? 

When I send the array from matlab to labview using the fprintf command (in matlab), it is as a string, but matlab has stored the values as double precision numbers before sending them. I've changed that by including a decimal limiter in matlab to the fprintf command fprintf(u,'%.4f\n',score); where u is the udp client object in matlab. Are you suggesting that I remove the format? 

 

I've gone ahead and included a photo of the entire system. The command window on the left shows the arrays being calculated in the matlab engine and the probe display shows the values in labview being read in as a string (I've gone back to the string to byte array form). 

 

The byte array is in decimal form correct? Can I use that to get my array back? 

 

Edit: included the VI as well

Download All
0 Kudos
Message 5 of 10
(3,826 Views)

When I use the fwrite command in matlab without a format indicator, I get binary values output to labview (first picture). When I specify a precision (single, for example), I get a different kind of data output to labview (second picture). 

Can anyone explain to me what the data in the second picture is? It's supposed to be the three decimal values displayed in the command window on the left side of the photo. 

Download All
0 Kudos
Message 6 of 10
(3,812 Views)

@traviskv wrote:

When I use the fwrite command in matlab without a format indicator, I get binary values output to labview (first picture). When I specify a precision (single, for example), I get a different kind of data output to labview (second picture). 

Can anyone explain to me what the data in the second picture is? It's supposed to be the three decimal values displayed in the command window on the left side of the photo. 


I sure can!  Those are single precision numbers represented as bytes.  Three numbers X 4 bytes per single precision number = 12 bytes.  🙂

byte array to single.png

Seems like you're doing everything right so far!

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 7 of 10
(3,807 Views)

Thank you, billko. That makes a lot of sense. 

I tried following your example. I don't get an error, but I get a different answer. I've attached my VI and a screenshot.

Is this because I am trying to read in an 1x3 array and instead it is being read in as one number? 

 

bytearraytosingleexample.PNG

Download All
0 Kudos
Message 8 of 10
(3,793 Views)

You have a coercion dot on your array of single. This indicates that you did not make your type array as double. Change it to double and you should get the correct results. 

Message 9 of 10
(3,777 Views)
Solution
Accepted by topic author traviskv

The simplest way to duplicate the code in the picture... is to duplicate the code!  😄

 

The picture of the code in my other post is magical!  Well, okay, it's a png file with metadata included that, when dropped on to the block diagram of a VI, converts it into real code.  But it's almost like magic.  😉

 

So click on the picture - it's called a "snippet" - and download it.  Then open up a blank VI and drag that png file into your block diagram.  If you notice, the little text on the right side says "2014" so you'll have to have LV 2014 or newer to make this work.  (Although snippets have been around a lot longer than that.)

 

Try it out!  🙂

 

edit: seems like I only did the first number in my snippet, but you already have the idea.  🙂

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 10 of 10
(3,766 Views)