LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Quantity and Repetition of array

Solved!
Go to solution

I'm new to Labview and would like some assistance please.

 

I need to count quantity of a each item with duplicates in both. Meaning, if I have a 2D array:

Quantity       Item

1                  Tomato
2                  Tomato
3                  Tomato
1                  Apple
1                 Orange

 

I would like the resulting table to be:

6                   Tomato
1                    Apple
1                   Orange

 

I've figured out how to count duplicates of a single column, but I can't figure out how to incorporate the Quantity into it.

 

 

0 Kudos
Message 1 of 9
(4,835 Views)

What is the format of the File?  I'm assuming it is strings of the form "3 apple", i.e. a number and an item name, in separate lines.  I'm assuming that you can read this file and end up with an array of Strings, all of this form.

 

So look at the String -- it consists of a Number and a String.  Now look at the Scan from String function (I recommend always using this instead of the seemingly-simpler String Conversion functions).  It is designed to let you get (in this case) two outputs -- a number (3) and a string (apple).  Try that.  You'll end up with a single For loop inside of which is a single Scan from String function, and when you are done, you'll have two arrays, one of numbers and one of items (or, if you want, you could combine the Number and Item into a cluster, then you'd have an array of clusters).

 

Bob Schor

0 Kudos
Message 2 of 9
(4,824 Views)

Dear Rob,

 

Thank you for your response. You are correct, the input will be a 2D string. I plan to read two columns from an excel file. I've attached a simplified VI with the 2D string array that I will be using. 

 

I've never used the "Scan from String" function and I'm having a bit of trouble with it. I'm getting an error stating "Scan failed. The input string does not contain data in the expected format.". Any suggestions on how to deal with this error?

0 Kudos
Message 3 of 9
(4,791 Views)

@Zeustra wrote:

Dear Rob,

 

Thank you for your response. You are correct, the input will be a 2D string. I plan to read two columns from an excel file. I've attached a simplified VI with the 2D string array that I will be using. 

 

I've never used the "Scan from String" function and I'm having a bit of trouble with it. I'm getting an error stating "Scan failed. The input string does not contain data in the expected format.". Any suggestions on how to deal with this error?


Pretty close

I did it very similar like so

 

Capture.png

 

to extend it with your 2D array

Capture.png


"Should be" isn't "Is" -Jay
0 Kudos
Message 4 of 9
(4,770 Views)

Thank you Jeff.

 

I seem to be having one last problem. I think it's with the way the array is combined. This is what I'm getting:
Results

 

I think the issue stems here but I'm not exactly sure:

Build.PNG

 

May I trouble you with a bit more advise on how to resolve this issue?

0 Kudos
Message 5 of 9
(4,749 Views)

I should clarify, the data in column one will be inputted on different days. Meaning:

Day 1: (1 Tomato)

Day 2: (2 Tomato)

Day 3: (4 Tomato)
Day 3: (1 Apple)

 

I will input in the 2D array: 

(1 Tomato)

(2 Tomato)

(4 Tomato)

(1 Apple)

 

Result I need from VI:

(7 Tomato)

(1 Apple)

 

I hope this makes sense

 

0 Kudos
Message 6 of 9
(4,732 Views)
Solution
Accepted by RavensFan

Now you have me very confused.  You just said:


(1 Tomato)

(2 Tomato)

(4 Tomato)

(1 Apple)

This is a 1D array of strings, the first entry being "1 Tomato", the last being "1 Apple".  This is what I thought you were working with.  The code "Count Duplicates" you attached, however, shows a 2D array of strings, with column 1 being numbers, column 2 being Items.

 

Looking at your code, you clearly do not understand how Arrays and For Loops work.  Your example shows a 2D array with 4 rows and 2 columns.  You bring it into a For Loop using an Indexing Tunnel (good!).  This makes the For loop "loop" 4 times.  The first time, you get Row 0, the first row, and the fourth time you get Row 3, the last row.  Each row is a 1D array with 2 elements.  If you use the Index Array function (which you do), the first time through the loop, you'll get Column 0 of Row 0, the string "3".  The second time, you'll get Column 1 of Row 1, the string "Tomato".  The third time, you'll get an empty string because you are trying to return Column 2, which doesn't exist, of Row 2.  This is clearly wrong.

 

As for Scan from String, right-click the function on your Block Diagram, and select Help.  Read the Help description of Scan from String.  Notice that there are inputs on the left below the Error Wire -- do you understand what they are for?

 

Scan from String can also be used in the way I thought you were meaning to use it, namely on a 1D array whose entries are strings like "2 Tomato", to give you both the integer 2 and the string "Tomato".

 

Write yourself a little VI that has nothing in it except a Scan from String function.  Right-click the upper-left connection (where Input String would go) and choose "Create Constant".  In the blank String constant, write "2 Tomato".  Now read the help, figure out (from the Help) how to get the Function to write the Format String for you.  Finally, wire indicators to the two output function (call the top one "Number" and the bottom one "Item" -- the top should automatically be an I32 integer, the bottom a string).  Run this.  Oh, also right-click Error Out and say "Create Indicator" so you can see if you get an error.

 

When you get so it gives you 2 and "Tomato", you'll understand how this function work.  Read the Help carefully.  Try simple "example" VIs to test your understanding.  Teach yourself LabVIEW ...

 

Bob Schor

0 Kudos
Message 7 of 9
(4,721 Views)

Dear Rob,

 

Thank you. I'm quite new to LabVIEW, thank you for the helpful learning hints. I tried your method of using the "scan from string" function in a VI by itself and I generated the number and string separately. I'll keep this trick in mind for future endeavors.

 

In case this helps anyone else in the future, here's the solution to the problem I described above.

0 Kudos
Message 8 of 9
(4,696 Views)

I'm sorry to disagree with you (I'm wearing my Professor Hat now) but that is not the "solution to the Problem you described".  The Problem you described, as I stated previously, involved a 1D array of strings, like this:Input Strings.png and should produce an output Array of Strings like this:
Output Strings.png

A "good" solution would grow linearly with the size of the input (so a million entries would take 1000 times as long as a thousand entries).

 

As this is clearly a School Assignment, I'm not going to produce my solution, but I'll describe it.  It starts with the 1D array of entries, as above (I actually used this Constant Array in my code).  A For Loop converts this (using Scan from String and Bundle) into a Cluster of Item (the string) and Count (the number).  Because Item is first, I can sort the resulting 1D Array of Clusters and get all the Apples, Oranges, and Tomatoes together.  One more For Loop looks for "Is this Entry different from the last one?" and accumulates totals, which it "exports".  [There is a tricky part when doing pair-wise array element comparisons at the beginning and end of the array -- try it out on paper, see if you can identify the problem, and then see if you can figure out a simple solution.  If you need a hint, it is called a "Sentinel"].  Finally, the last For loop takes the Array of Clusters that constitute the Totals and uses Format Into String (the inverse of Scan from String) to create the final String Array of Totals shown above.

 

Bob Schor

Message 9 of 9
(4,665 Views)