LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert 2D array of cluster of 4 elements to 2D array of double precision values, or to a 1D array

Solved!
Go to solution

Is there an easy way to convert a 2D array of clusters (4 elements each including a boolean and three dbl numbers) to a 2D or 1D array of double precision values? I need to do this in order to store my data to a text file to control a robot I'm working on.

 

Thank you.

0 Kudos
Message 1 of 11
(4,776 Views)
Since you just have the one boolean, why not make it a DBL, and just look for 1 or 0. It's an overkill datatype for the data, but it gets out of the cluster and into a normal array.
Richard






Message 2 of 11
(4,759 Views)
Solution
Accepted by topic author chuck72352

There's a million different ways to do this. The easiest thing to do would be to leave the data exactly as it is and just use Flatten to XML (search the palette). It accepts virtually any data type, including a 2D array of clusters as you described. It will spit out human-readable (though not necessarily pretty) text that you can write to a text file. Then you read the data from the file and use Unflatten from XML to do the reverse operation.

 

Or you can employ a similar scheme to flatten your data into a 1D array of doubles that you can then unflatten.

 

Message Edited by Jarrod S. on 03-04-2009 03:02 PM
Jarrod S.
National Instruments
Message 3 of 11
(4,754 Views)

Make a change to the cluster and you've got 5 or 6 changes to do to the program. Seems like arrays, written to spreadsheet file, would be easier! He didn't say he had to read it back in!  Smiley Happy 

Richard






Message 4 of 11
(4,742 Views)

Thanks! Both approaches work! Problem solved!

0 Kudos
Message 5 of 11
(4,740 Views)

One last point. Since the final size of your flattened array is known (2 + 4*NumRows*NumColumns), it's better to preallocate your array of doubles rather than using Build Array in a loop. You'll get dramatically better performance as your 2D array grows.

 

 

 

 

Message Edited by Jarrod S. on 03-04-2009 04:39 PM
Jarrod S.
National Instruments
Message 6 of 11
(4,718 Views)

I tried it and it does make a difference.

Thanks.

0 Kudos
Message 7 of 11
(4,713 Views)

Things look pretty good except for some minor code cleanup. 🙂

 

For some reason, the following code is about 20% faster overall, mostly because the reverse operation seems to be significantly faster this way, and I am not quite sure why. 😉

 

Message Edited by altenbach on 03-04-2009 09:48 PM
Message 8 of 11
(4,684 Views)

Since all you want is write a text file, it seems useless to generate all these complicated data structures in memory. Why not wite to text file and later read it back directly.

 

Here's a simple draft. Modify as needed. 😉

 

 

(I am not sure about overall performance. There are probably tweaks possible)

Message Edited by altenbach on 03-04-2009 11:50 PM
Message 9 of 11
(4,669 Views)

Excellent work as always, Altenbach! I have heard there is a penalty for creating 2D arrays using auto-indexing from nested for loops. I think this has to do with making sure each row in the 2D array is the same size, even though in this case they should have to be. Your example probably avoids that penalty by creating a 1D array and reshaping it afterwards. This doesn't require iterative size checks.

Message Edited by Jarrod S. on 03-05-2009 08:16 AM
Jarrod S.
National Instruments
Message 10 of 11
(4,649 Views)