LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I extract "sandwiched" subarrays based on repeated predefined numbers in LabVIEW?

Hi, I'm trying to extracts subarrays from a 1D input array based on a "sandwich rule". The input consists of a mix of predefined numbers (like 1, 2, 3, 4) and a generic character "S". The "sandwich rule" has the following requirements:

 

When either the generic character (S) or one of the predefined numbers appears that defines the beginning of the sandwich.
When two of the same predefined number appear consecutively (e.g. 2,2), that defines the end of a sandwich.

 

The output should be a new row (in a 2D array) containing all elements from the start to the end of the sandwich

 

I have attached a screenshot containing example inputs and outputs and the VI containing these elements. 

In this case the output is 

Row 1: S R R 1 R R 2, because S and 2 create the sandwich,

Row 2: 2 R R R S, because 2 and S create the sandwich.

Asasafuchi_0-1750755901391.png

 

0 Kudos
Message 1 of 16
(604 Views)

Shouldn't the 1 begin a new sandwich? Or once a sandwich is started it continues until double numbers or S?

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

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 2 of 16
(588 Views)

Yes, once it is started it continues until double number or S appears. Since S started the sandwich the double 2s "terminate" the sandwich. IF however there was no S in the beginning, the 1 would start the sandwich as you say.

0 Kudos
Message 3 of 16
(584 Views)

Then i'd make a state machine. 

State 1; Find start: Looks for 'S' or 2 identical numbers from the list (String subset), save the index in a shift register.

State 2; Find End; Similar to State 1, save index (or combine with state 3)

State 3; Extract string: Extract the string between Start index and End index.

State 4; Repeat from State 1 with a new start index (end+1)

Repeat until String is empty.

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

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 4 of 16
(553 Views)

Thanks for the suggestion. What would be the easiest way to find the S or the two identical numbers given that we dont know which one will appear first (or at all)?

0 Kudos
Message 5 of 16
(530 Views)

How long will your input array be? Will it be the same length every time? Will you ever get partial messages so you need to carry over data?

Tim
GHSP
0 Kudos
Message 6 of 16
(515 Views)

Hi, the array wont be the same length every time. I'm not sure what you mean by partial messages but the array will be complete every time.

0 Kudos
Message 7 of 16
(510 Views)

This should help you get started.

Yamaeda_0-1750773887451.png

 

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

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 8 of 16
(507 Views)

@Asasafuchi wrote:

Hi, I'm trying to extracts subarrays from a 1D input array based on a "sandwich rule". The input consists of a mix of predefined numbers (like 1, 2, 3, 4) and a generic character "S". The "sandwich rule" has the following requirements:

 

When either the generic character (S) or one of the predefined numbers appears that defines the beginning of the sandwich.
When two of the same predefined number appear consecutively (e.g. 2,2), that defines the end of a sandwich.

 

The output should be a new row (in a 2D array) containing all elements from the start to the end of the sandwich

 

I have attached a screenshot containing example inputs and outputs and the VI containing these elements. 

In this case the output is 

Row 1: S R R 1 R R 2, because S and 2 create the sandwich,

Row 2: 2 R R R S, because 2 and S create the sandwich.

 

 


You need to better define the problem.

 

First of all, LabVIEW does not support ragged 2D arrays, thus all rows are the same length. You might want the output as 1D array of strings.

 

Can you guarantee that all strings are single characters? In that case, the input should probably be a singe multi character string, right?

 

It is confusing if you show examples where the output has spaces between characters. In your example (Row 1) S and 22 create a sandwich, (not S and 2). Row 2 is not a sandwich because it does not end in a double character.

 

I assume terminators in this case are only 11, 22, 33, 44, S (and not also e.g. 12, 43, 24, S3, 2S, etc.)

If a "1" starts a sandwich, must it end in 11 or S, or would it also end in 22, 33, 44?

 

So should the output "sandwich" include the start tag and only one of the termination tags? What should happen to the second termination character?

 

I recommend to resize array containers so the first invalid element shows (or show the scrollbar), else we cannot tell how long the arrays really are.

 

Why are your predefine numbers DBL? I assume they must be single digit integers, right? What should happen if one of them is 3.14159?

 

I would redefine the input and output datatyles as follows:

 

3 strings as input (make sure to "limit to single line)

1D array of strings as output.

 

altenbach_0-1750778007334.png

 

0 Kudos
Message 9 of 16
(491 Views)

@Asasafuchi wrote:

Thanks for the suggestion. What would be the easiest way to find the S or the two identical numbers given that we dont know which one will appear first (or at all)?


Feed a regular expression (q.v.) to the Match Pattern function.

0 Kudos
Message 10 of 16
(487 Views)