From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Writing to an uninitialized string array?

Solved!
Go to solution

If I have a 2D uninitialized string array and want to write a str to let say cell 5.2. How should I do it?

Further, let say I want to add more data into the array at cell 7,5. How should that be done?

 

(The empty cells can be filled with Empty String Const.)

 

0 Kudos
Message 1 of 9
(1,328 Views)
Solution
Accepted by topic author TakeANap

You can use the "Max & Min" function to determine easily the needed new array size:

 

Adapt 2D-Array size to new elmnt Idx.png

 

If needed, you create a new array with the needed size and copy the original part into the new larger array. Then replace the new element at the given index position.

Greets, Dave
Message 2 of 9
(1,315 Views)

I believe the simple answer to your question is that you cannot manipulate "what isn't there", meaning you need to first initialize your String Array (which sets its size, and lets you define what you mean by "Cell 7, 5" (does this mean the 6th element in the 8th row, considering the initial element is at (0, 0)?).

 

Bob Schor

0 Kudos
Message 3 of 9
(1,313 Views)

Some additional hints:

if your array is of significant size and you want to insert many elements at outer ranges, it is better to initialize your array to the max assumed size, insert the new elements with the "In Place Element Structure" and shrink the array afterwards (Subarray).

Greets, Dave
Message 4 of 9
(1,307 Views)

You confirm the method i use. It is similar as mine. I add with Insert array a subarray below or to the right of the last free row/col. 

I have also add code that detect first free row/col and use that value as xy-parameter for the new data. I did that for fun.

 

 

0 Kudos
Message 5 of 9
(1,290 Views)

Me too 😁

Greets, Dave
0 Kudos
Message 6 of 9
(1,279 Views)

@TakeANap wrote:

You confirm the method i use. It is similar as mine. I add with Insert array a subarray below or to the right of the last free row/col. 

I have also add code that detect first free row/col and use that value as xy-parameter for the new data. I did that for fun.

 

 


For Insert Into Array, if you leave the index inputs unwired, it automatically adds the new data to the end. No need to get array size and wire the inputs.

Redhawk
Test Engineer at Moog Inc.

Saying "Thanks that fixed it" or "Thanks that answers my question" and not giving a Kudo or Marked Solution, is like telling your waiter they did a great job and not leaving a tip. Please, tip your waiters.

0 Kudos
Message 7 of 9
(1,256 Views)

@TakeANap wrote:

I add with Insert array a subarray below or to the right of the last free row/col. 

I have also add code that detect first free row/col and use that value as xy-parameter for the new data. I did that for fun.


Your "method" requires significantly more code because you need to determine if a subarray needs to be inserted below, to the right, or even both. I don't understand what the "fun" in that would be. A Rube Goldberg cartoon perhaps? 😄 Dave's solution is significantly better and more efficient.

 

The correct solution would be to decide from the beginning on a reasonable maximum size, initialize an array of empty strings at that size and keep the size fixed. Memory management is significantly better if arrays don't constantly change size. Also the code is greatly simplified.

 

Note that arrays of any dimension are stored linearly in contiguous memory, so inserting columns typically requires a complete new allocation for everything because most elements need to be moved to new positions relative to the first element (have a look at slide #11 here  (in part II)).

 

You did not explain the purpose of all this! For example if the 2D array is typically "sparse" (only very few elements are not empty strings), you might want to use a MAP with an array of indices as key and a string as value. Now you can even add an element at  Index [2147483647, 2147483647] without much memory penalty and can look it op at any time later. All elements that are empty strings are default and don't need to be part of the map. It is safe to say that a 2D array of empty strings with 10^18 elements is not possible. You could even make the "key" a pair of U64 to perfectly store an string value with indices 18446744073709551615 each. 😄

 

 

 

Message 8 of 9
(1,222 Views)

Your comment was exactly what I was looking for when I posted my question. I will look into your suggestions.

 

The VI I'm speaking about isn't used in a time-critical application. However, if I would be writing code for ha highspeed sampling engine I would have used another way of allocating memory and storing data.  I working also with embedded microcontrollers and forget that PC:s has lots of memory. 🙂

 

0 Kudos
Message 9 of 9
(1,205 Views)