LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

any examples of vi scripting an array??

I am trying to use VI scripting to build an array.  I cannot find any examples at all 😞

 

In my VI, I read data from a text file into an array.  But the data is raw, and I need to repackage it into a 2d array to use in another VI (this other VI expects the data to be in a 2D array of string format).

 

The way I would do this if scripting wasn't required, would be to use the build array function and simply connect up the wires.  Since I can create a Build Array object and get access to its terminals, I figured the easiest way to script would be to just loop and connect the wires.

 

So to continue scripting, I need to be able to create a new VI Object of type array, and assign it's value to be the known value I am extracting from the text file.  This is where my problem comes in.  I cannot determine how to do this.  I am experiencing 2 problems:

 

array.png1. I first assumed that I need to specify the size/dimensions of the array I'm trying to create.  So I used the Number Of Columns/Dimensions/Rows property to do so.  However, Labview had a problem with each of those.  I simply wired a constant number to the property and it said, "The Specified Object Was not found.  Property Name: Number of Columns"

 

2. Even without doing that, I thought, maybe if I just wire up the data, it will automatically recognize the dimensions, etc.  But the 'value' property is of type Variant, and no matter what I do I get an error 91.  I first tried wiring up the value called Array (you can see the indicator) to it, but that didn't work.  You can see in the pic below that I took a simple case of just doing To Variant on a constant 0, but it still gives me error 91.

 

I cannot find examples anywhere.  Can someone point me to one or make some suggestions?

 

Thanks...

0 Kudos
Message 1 of 13
(4,460 Views)

Why are you using scripting to do this?

 

It sounds like your task is reading a file, and building a 2-D array from it.  You say "the data is raw".  What does that mean?  You want to repackage it into a 2-D array.  Show us before and after pictures of what the data should look like.

 

It seems like LabVIEW would have plenty of ordinary functions for what it sounds like you are doing without resorting to scripting.

0 Kudos
Message 2 of 13
(4,436 Views)

well, before is a text file, after would look something like below:

array.png

 

This 3D array (output from the build array node at bottom right) is what goes to my event structure for processing.

 

So I need to read from a text file, and create these arrays accordingly.  It all needs to happen automatically, i.e. I can't be wiring things up by hand (there are too many...above is just a small example I created by hand).

 

The spreadsheet string to array function grabs 4 pieces of data off of one line (i.e. 1, 1, rw, bit-name) and makes it into an array.  But now I need to put that into another array and then one more, all automatically.

0 Kudos
Message 3 of 13
(4,433 Views)

That's what For Loops will do for you.  You just need to auto-index on the output tunnel.

 

The only question you need to answer is how many times does each For Loop need to run.  What you show is taking 4 elements and building into a 1-D array, then taking 4 1-D arrays and making a 2-D array, then taking 4 2-D arrays and making a 3-D array.

 

Is it always a 4 to 4 to 4 relationship?  Could you windup up needing more rows, or columns, or pages in a 3-D array that depends on the data within the file?

 

So far what you are showing is basically trying to use VI scripting to programmatically generate a piece of code that would get nominated for the Rube Goldberg thread.

0 Kudos
Message 4 of 13
(4,427 Views)

no, it won't always be consistent...there will always be 4 entries in the lowest level array, but after that it can vary...

 

Believe me I understand what you mean, I too looked at this and thought there must be an easier way.  One thing that led me down this path is that when you use the build array function, what I found was that even when I deleted entire elements out of the lowest level of array, it still let me connect without concatenating inputs.  See the pic below and note that the build array on lower right is not concatenating inputs:

array.png

 

So before my initial post, what I tried was to simply use shift registers in conjunction with the build array function.  This should work no problem, except it is forcing the build-array concatenate option to be checked and I can't un-check.  So my thinking was, if I could write a script to connect things the same way I would by hand, then it should work.

 

The other thing is, and correct me if I'm wrong, but my VI Script (let's call it script.vi) is creating a new VI (let's call it new.vi) with all these controls and indicators in it and such.  That's all working fine.  But if I build the actual array using loops and such, then it's just creating the array itself inside of script.vi, not new.vi.  Do you understand what I'm trying to say?  In other words, ultimately I need a properly formatted array to be in new.vi after the script runs.

0 Kudos
Message 5 of 13
(4,420 Views)

You do realize that scripting won't work on the block diagram of a running VI, don't you?  You cannot dynamically change the way a VI is written while it is running.

 

Kelly Bersch
Certified LabVIEW Developer
Kudos are always welcome
0 Kudos
Message 6 of 13
(4,410 Views)

@bmishoe wrote:

no, it won't always be consistent...there will always be 4 entries in the lowest level array, but after that it can vary...

 

How do you determine what the size of the other two dimensions would be?  Let's say the 4 elements fill up a row.  Then you take X number of row to make a 2-D array.  When do you stop adding rows to a 2-D array then start putting them at row 0 of the next page in the 3-D array?  Is there anything in you data file that determines this?  Do you just arbitrarily pick a number?

 

Believe me I understand what you mean, I too looked at this and thought there must be an easier way.  One thing that led me down this path is that when you use the build array function, what I found was that even when I deleted entire elements out of the lowest level of array, it still let me connect without concatenating inputs.  See the pic below and note that the build array on lower right is not concatenating inputs:

 

Correct.  It is not concatenating inputs.  It is taking multiple 2-D arrays and building them up as pages of a 3-D array.  I don't know exactly what you mean by "deleted entire elements out of the lowest level of the array".

 

So before my initial post, what I tried was to simply use shift registers in conjunction with the build array function.  This should work no problem, except it is forcing the build-array concatenate option to be checked and I can't un-check.  So my thinking was, if I could write a script to connect things the same way I would by hand, then it should work.

 

You'll have to attach your code where you tried this for me to tell you exactly what the problem was.  If you are building arrays in a loop with shift registers, you will need to use Replace Array Subset as you add elements into the next row of a 2-D array, or replace rows in the next page on a 3-D array.  You can only uncheck the concatenate option of you are using Build Array on two arrays that have the same number of dimensions.  There you have 2 choices, take two 2-D arrays and concatenate them end to end for a larger 2-D array.  Or you can build them into a 3-D array, thus adding another dimension.   You can "stack" a 1-D array onto a 2-D array to create a different page of a 3-D array , the only thing you can do is concatenate it to the end of a 2-D array to get a larger 2-D array.

 

The other thing is, and correct me if I'm wrong, but my VI Script (let's call it script.vi) is creating a new VI (let's call it new.vi) with all these controls and indicators in it and such.  That's all working fine.  But if I build the actual array using loops and such, then it's just creating the array itself inside of script.vi, not new.vi.  Do you understand what I'm trying to say?  In other words, ultimately I need a properly formatted array to be in new.vi after the script runs.

 

Generally when you read a file and want to create an array, you don't try to create an array object into that VI or into another VI.  You operate on the data that is on the wire.  If you want to do something in a different array, then you wire it up so you pass the data into the other VI through its connector panel.

 



It feels like you've forgotten some LabVIEW basics and are looking for the biggest hammer you can find to do basic array operations that LabVIEW is used for every day.

 

Don't overcomplicate your problem.  Take a step back.

 

If the data file is essentially a 2-D array as it is,  multiple rows of 4 items per row, just use Read Spreasheet File to get a 2-D array.  Then if you want a 3-D array, use Reshape array to restructure into that.  The only complication might be that the resulting 3-D array may not be exactly what you want.  Rows may become columns, and vice versa.  If so, Transpose Array on the original 2-D array could help.

Message 7 of 13
(4,403 Views)

RavensFan wrote:


It feels like you've forgotten some LabVIEW basics and are looking for the biggest hammer you can find to do basic array operations that LabVIEW is used for every day.


I couldn't agree more.  This is what for loops are designed for.  To run a set number of times that may not be known until runtime.

Message 8 of 13
(4,390 Views)

Thanks for all the help and suggestions.  You've given me a lot of good ideas to try out.

Message 9 of 13
(4,359 Views)

Hi,

 

Where can I find the "GrowableFunction" class please ? I need it and i'm not abble to find it when I right-click on "VI object class".

 

Thx.

0 Kudos
Message 10 of 13
(4,176 Views)