NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I fill an array created as a local variable?

I have created an array of strings as a local variable and I want to fill this array with values.  How do I fill the array?  I tried using pre-expression to do this but I cannot determine the correct syntax. 
 
 Is this the correct way of doing this or is there another method?
0 Kudos
Message 1 of 11
(6,968 Views)

Hi,

Have a look at the example AccessingArrays in the TestStand\examples folder.

The thing to remember is the index to the array is syntax as "[index]" eg "[0]" and similar 2d arrays its "[0][0]"

hope this helps

Regards

Ray Farmer

Regards
Ray Farmer
0 Kudos
Message 2 of 11
(6,961 Views)
Hi,
 
Thanks for the help but, I couldn't get it to work.  I took a look at the examples and I could not find any example that used the indexing method as you described.  What I am trying to do is use the pre-expression to fill an array:  Something like this:
 
Locals.MyArray[0] = "String"
 
But this does not work.  Is there any method to do it inside of Teststand without calling a labview vi?
 
Thanks.
0 Kudos
Message 3 of 11
(6,953 Views)
It works just like your example unless, of course, you have defined the array to be initially empty. In which case, you have to do a SetNumElements first. When you do it in an expression, do you get an error message? What's the message?
0 Kudos
Message 4 of 11
(6,945 Views)
Thank you so much.  That is exactly what the problem was.  I was initializing the array to be empty.  Thanks a lot.
0 Kudos
Message 5 of 11
(6,942 Views)
May I interject for a moment?

I think the question of syntax needs to be addressed further.

If you want to assign values to an empty 1D array, you can use "Locals.MyArray = {1, 2, 3, 4, 5}"

However, is there a way to assign values to an empty 2D array without having to assign each row separately?

Thanks,
Joel
0 Kudos
Message 6 of 11
(6,622 Views)
Hi Joel,
 
Currently, while you can assign empty 1D arrays using the syntax you listed, you cannot use this syntax for empty 2D arrays.
 
I will file a product suggestion for this so it will be considered for future versions of TestStand.
 
Matt M.
NI
0 Kudos
Message 7 of 11
(6,599 Views)

You can initialize arrays of arrays with nested array syntax:  locals.x = { {1, 2}, {3, 4}}

However, currently there is no equivalent syntactical shortcut for initializing a multidimensional array (which is not the same thing as an array of arrays).

For a multidimensional array, you could:

- set each element in the editor

- explicitly initialize each element (perhaps in a long comma separated statement)

- write a loop (which could copy values from a single dimensional array or an array of arrays you initialize separately)

- pass the multidimensional array to a code module and initialize it there

 

 

Message 8 of 11
(6,599 Views)
Are arrays of arrays a capability of a TestStand 3.1?  I don't currently see a way to create them.  All I see is the capability to create multidimensional arrays.  This brings up another question.  For practical purposes, can't a multidimensional array be thought of as an array of arrays?

At least when coding such in C, one would first allocate memory for the all the rows:

TestArray = calloc (numRows, sizeof(double*));

And then allocate each row individually, in a loop:

TestArray[i] = calloc (numCols, sizeof(double));

Then you would index an array element with two dimensions, TestArray[x][y].  So, unless TestStand handles this differently, I don't see how a multidimensional array is different from an array of arrays, conceptually.  If I have missed anything, I await further enlightenment.
0 Kudos
Message 9 of 11
(6,558 Views)

Both in TestStand and C/C++, multidimensional arrays are different from arrays of arrays in that multidimensional arrays must be "square" (non-jagged) because element access is computed based on the full array dimensions. In contrast, arrays of arrays can be jagged. Element access is implemented by obtaining a reference or pointer to another array from a containing array, until you reach the last array which contains the elemental datatype.

To create an array of arrays in TestStand, create an array custom data type. Then create a variable that is an array of that custom datatype. By default your array of array will be "square", but you can resize individual sub-arrays to make it jagged.

 

Message 10 of 11
(6,554 Views)