cancel
Showing results for 
Search instead for 
Did you mean: 

Array subset function - am I just missing something obvious or does it not exist

SOLVED
Highlighted
lv_newb
Member
Solved!

Array subset function - am I just missing something obvious or does it not exist

Hi all,

LV Newb here, but been messing around with programming languages since the 90s. Finding it really hard to get my head around LV, which is frustrating, and I can't find things that I expect should be there. I have spent ages looking through Google and YouTube and am annoyed enough at the lack of answers that I have signed up just to ask a question. Hopefully one of you can help with what should be a really simple answer.

 

I have an array of U8, I want to subset it and flatten (e.g. I have [1,1,0,0,1,0,1,0] and I want the top 4, I want to subset 0:3 and get out "1100"). I have done stuff like this over the years in various languages so know it is possible. Is this not easily achieved in LV with a function? I have tried various things to get it to work without success. 

 

Any advice is appreciated, thank you.

LVN

12 REPLIES 12
jcarmody
Trusted Enthusiast

Re: Array subset function - am I just missing something obvious or does it not exist

Message contains an image

jcarmody_0-1674045073876.png

 

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

lv_newb
Member

Re: Array subset function - am I just missing something obvious or does it not exist

Hi,

Thanks for your reply, and apologies that I didn't make it more clear in my original question.

 

I am already using Array subset, but then getting an array. Continuing my example, I get [1,1,0,0]. I can't find a way to get out "1100", either directly or any kind of data conversion. I have tried "flatten to string", "type cast", "byte array to string" etc. without any luck.

 

Regards,

LVN

GerdW
Knight of NI

Re: Array subset function - am I just missing something obvious or does it not exist

Hi newb,

 


@lv_newb wrote:

I am already using Array subset, but then getting an array. Continuing my example, I get [1,1,0,0]. I can't find a way to get out "1100", either directly or any kind of data conversion. I have tried "flatten to string", "type cast", "byte array to string" etc. without any luck.


The typical way to calculate a numeric value from a bunch of bits is this:

x = sum( bit(i) * 2^i) (for all bits in your array)

 

Use an autoindexing loop to access the array elements…

 

The more LabVIEW-style approach is to use the BooleanArrayToNum function!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
pincpanter
Trusted Enthusiast
Solution

Re: Array subset function - am I just missing something obvious or does it not exist

Message contains an image

One of various possibilities (if you want a string, it's not that clear...):

aaaaa.png

Paolo
-------------------
LV 7.1, 2011, 2017, 2019, 2021
lv_newb
Member

Re: Array subset function - am I just missing something obvious or does it not exist

Thanks GerdW,

 

OK, so it sounds like there is no built-in one-stop function to extract the values. In which case I will do as you suggest, create a subset then loop through, indexing each element and concantenating them together.

 

For my purposes I need to retain them in bit representation (e.g. 1100 as opposed to its decimal numerical equivalent).

 

Thank you for the advice.

LVN

lv_newb
Member

Re: Array subset function - am I just missing something obvious or does it not exist

oh wow, pincpanter, that is exactly what I wanted! I just never thought I would have to have some many intermediary steps!

ZYOng
Trusted Enthusiast

Re: Array subset function - am I just missing something obvious or does it not exist

Comment deleted.

-------------------------------------------------------
Applications Engineer | TME Systems
https://tmesystems.net/
-------------------------------------------------------
https://github.com/ZhiYang-Ong
GerdW
Knight of NI

Re: Array subset function - am I just missing something obvious or does it not exist

Hi newb,

 


@lv_newb wrote:

I just never thought I would have to have some many intermediary steps!


That are just 3 steps…

 


@lv_newb wrote:

I have an array of U8, I want to subset it and flatten (e.g. I have [1,1,0,0,1,0,1,0] and I want the top 4, I want to subset 0:3 and get out "1100").


You want to

  • take a subset of a numeric array
  • convert the elements from numeric to string (which you forgot to mention in your first message)
  • concatenate those elements to a single string (which is not really clear from your first message)

Each step is done with just one function…

 


@lv_newb wrote:

 I have [1,1,0,0,1,0,1,0] and I want the top 4, I want to subset 0:3


You really need to learn to write your questions more accurate!

  • Arrays start with element 0: when you want the "top 4" elements (aka subset 4:7) that would be "1, 0, 1, 0" in my opinion…
  • Subset 0:3 are the first 4 elements from your array, representing the lowest bits in a boolean array…

General suggestion:

Always attach a VI with your input data stored in a control and with an indicator showing the expected output result. This always helps to determine the expected/required input and output datatypes!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
altenbach
Knight of NI

Re: Array subset function - am I just missing something obvious or does it not exist

Message contains an image

@lv_newb wrote:

I have an array of U8, I want to subset it and flatten (e.g. I have [1,1,0,0,1,0,1,0] and I want the top 4, I want to subset 0:3 and get out "1100"). I have done stuff like this over the years in various languages so know it is possible. Is this not easily achieved in LV with a function?


So what exactly is the end result you want? Which programming language would be able to do "stuff like this" in fewer steps?

 

In LabVIEW, you would wrap the solution into a SubVI for reuse. (U8 array input, String output) One single step in all future use!

 

So why exactly do you have an array of U8 instead of a singe U8 scalar that would be sufficient to represent 8 bits? Where does the data come from? Is the element at index 0 the LSB or the MSB? Typically the first element is LSB, so the result would be 0011, right?

 

Why do you want a string instead of a number in the end?

 

Can we please take a step back and describe the bigger context. How was the data before it became an U8 array of zeroes and ones? What should happen with the result?

 

Overall, you start with 8 bits of information inflated to 96bits (8x U8 + 32bit size header) and you want to covert half of it it to a string of ASCII characters (64bits, 42bit string data and 32bit size header). Seems Convoluted!

 

Here are some other hints:

 

altenbach_0-1674057905295.png