02-15-2024 10:38 AM
I want to do the following: convert my colorimetric cluster into a string array that I can ultimately write to a delimited spreadsheet VI
I'm having trouble understanding how to set the data type to do this
Solved! Go to Solution.
02-15-2024 10:52 AM
I can't seem to relate the subject of the topic to the question so I will ignore the subject. You do NOT have a "Cluster to variant question"; you have a "How do I convert the values of the elements inside my cluster to an array of strings" question. You will have to unbundle each element of the cluster and use the appropriate number to string conversion function in the String --> Number/String Conversion palette. This has nothing to do with variants.
02-15-2024 09:45 PM
Expanding on the comment made by @billko, your goal is poorly defined.
Consider the cluster you provided. You have two quantities that appear to be a value-unit pair (RadInt + RadIntUnit, and PhotInt/PhotIntUnit pair) and possibly a third (ScotopicInt and CalibrationUnit), which (despite the "Int", suggesting "integer") are a Dbl + String pairing, followed by 23 Dbls followed by an Array of Dbls (of unspecified size).
In the code fragment you provided, you convert it to a Variant. Why? You then follow this by a Variant-to-Data, but do not specify the format you desire for the data (this makes no sense, at least, not to me). Now, you could do what I just did and wire the Cluster coming from "Colormetric" into the top of the Variant-to-Data to get the Cluster back, which is pretty instructive:
Look at the indicator "data". If you get rid of the three Strings at the top, you have a 26 rows x 1 column array (which you can output with one "Write Delimited Spreadsheet" command, followed by a 1 row x ?unknown? columns array (which can also be output by one Write Delimited Spreadsheet" command. But what a mess ...
My advice is to go back to the drawing board and reorganize your Project. Are the "vertical" data (the 26 x 1 array) coming in at the same rate as the "horizontal" (1 x ?? array) data? Maybe the only seriously-time-varying data are the latter data points?
Bob Schor
02-16-2024 03:09 AM - edited 02-16-2024 03:12 AM
Maybe this helps:
Convert cluster to readable string and string to data, but not UTF-8? - NI Community
Maybe not 😁. Variants are a wild beast, hard to tame.
Converting a cluster to variant is simple (and quite pointless):
Converting a cluster (or variant of cluster) to an array of variants is quite useful:
You'd normally loop over the items, and convert them to string based on type.
However, this should be done in a recursive way, as a cluster can contain a cluster (arrays contain elements, but can contain clusters containing arrays, etc.). Enums are tricky too. Do you store the numbers or their strings? Both have there ups and downs.
Note that you can get away with one case for I8, I16, I32, I64 and one for U8, U16, U32, U64, and probably one for sgl, dbl, ext. Some for U8, U16, U32, U64 enums
Of course, if you don't have recursive types or don't want to support them, it's easier.
02-16-2024 04:30 AM
The normal/classic way of doing it:
02-16-2024 08:48 AM
Thank you to everyone the basic explanation of using variants, I find this forum to be an invaluable resource.
02-16-2024 09:25 AM - edited 02-16-2024 09:38 AM
I'd like to put in a good word for the MGI Read/Write Anything toolkit when you want to convert any arbitrary cluster into a human-readable string and later recover that cluster from the string.
The top level API function deals with .INI format files, but dig down just one level and you find nuggets like "String to Anything" and "Anything to String". (Note that these are nearly, but not quite symmetric functions, meaning that you can't quite run the output of one directly into the input of the other to recover your original data. S2A takes in a multi-line string, A2S puts out an array of single-line strings. Not the same, but pretty trivial to convert as needed.)
-Kevin P
02-16-2024 09:33 AM
Thank you
02-16-2024 12:56 PM
wiebe@CARYA wrote:
Maybe this helps:
Convert cluster to readable string and string to data, but not UTF-8? - NI Community
Maybe not 😁. Variants are a wild beast, hard to tame.
Converting a cluster to variant is simple (and quite pointless):
Converting a cluster (or variant of cluster) to an array of variants is quite useful:
You'd normally loop over the items, and convert them to string based on type.
However, this should be done in a recursive way, as a cluster can contain a cluster (arrays contain elements, but can contain clusters containing arrays, etc.). Enums are tricky too. Do you store the numbers or their strings? Both have there ups and downs.
Note that you can get away with one case for I8, I16, I32, I64 and one for U8, U16, U32, U64, and probably one for sgl, dbl, ext. Some for U8, U16, U32, U64 enums
Of course, if you don't have recursive types or don't want to support them, it's easier.
BD is slightly cleaner if you use "Get Cluster Information.vi" instead of "Variant to Data".
02-19-2024 03:53 AM
@paul_cardinale wrote:
wiebe@CARYA wrote:
Maybe this helps:
Convert cluster to readable string and string to data, but not UTF-8? - NI Community
Maybe not 😁. Variants are a wild beast, hard to tame.
Converting a cluster to variant is simple (and quite pointless):
Converting a cluster (or variant of cluster) to an array of variants is quite useful:
You'd normally loop over the items, and convert them to string based on type.
However, this should be done in a recursive way, as a cluster can contain a cluster (arrays contain elements, but can contain clusters containing arrays, etc.). Enums are tricky too. Do you store the numbers or their strings? Both have there ups and downs.
Note that you can get away with one case for I8, I16, I32, I64 and one for U8, U16, U32, U64, and probably one for sgl, dbl, ext. Some for U8, U16, U32, U64 enums
Of course, if you don't have recursive types or don't want to support them, it's easier.
BD is slightly cleaner if you use "Get Cluster Information.vi" instead of "Variant to Data".
But you loose the data in the elements.
Get Cluster Information only returns the variant data types with default data values.