03-04-2009 02:10 PM
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.
Solved! Go to Solution.
03-04-2009 02:56 PM
03-04-2009 02:59 PM - edited 03-04-2009 03:02 PM
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.
03-04-2009 03:12 PM
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!
03-04-2009 03:13 PM
Thanks! Both approaches work! Problem solved!
03-04-2009 04:39 PM - edited 03-04-2009 04:39 PM
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.
03-04-2009 04:41 PM
I tried it and it does make a difference.
Thanks.
03-04-2009 11:46 PM - edited 03-04-2009 11:48 PM
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. 😉

03-05-2009 01:49 AM - edited 03-05-2009 01:50 AM
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)
03-05-2009 08:12 AM - edited 03-05-2009 08:16 AM
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.