LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Using more than one data format for writing data to spreadsheet

I collect an array of data and eventually convert it to spreadsheet formatted data. Some of the data (by columns) is integer data and some is floating point. The specific issue I face currently is I want, say one column of floating point data to have six digits after the decimal point (or I could put it in exponential notation) but I want other floating point data to display only 2 digits after the decimal point. I know how to make a spreadsheet with one format for all data but in this case I want different formats for say different columns of data. Is there an easy way to accomplish this task?? I can certainly reformat the data after I output it in, for example Excel, but I want LabVIEW to take care of
the individual column formatting for me before (or while) I create the spreadsheet. I see sub vi's like "format into file" and so forth that might be what I want but I find the help files on that specific sub vi to be confusing at best... Any help would be appreciated... thanks... bob paris...
0 Kudos
Message 1 of 5
(3,566 Views)
There's nothing magic about spreadhseet format - you convert your numbers to text, separate the columns with tabs, and separate the rows with CR (CR/LF on Windows).

For your particular situation I would extract all of column 1 numbers into an array. Wire that array into NUMBER TO FRACTION STRING (in the string | String / Number Conversion palette) with a specifier of 6 - presto! you have an array of 6-decimal place strings.

Do the same thing with column 2, except format it for 2 dec. places. presto! you have an array of 2-decimal place strings.

Use BUILD ARRAY to make a two-dimensional array out of the two separate arrays.

Use TRANSPOSE 2D ARRAY to swap from {Col | Row} order to { Row | Col } order

Use ARRAY TO SPREADSHEET STRING
with a format specifier of %s to get it into a long string.

Create a new file, write this long string to the file, close the file, and you're done.
Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


LinkedIn

Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 2 of 5
(3,566 Views)
If your data is a 1D array of floating precision numbers, your simplest method is to use the "Format Into String" function. It provides unlimited flexibility for the format of each element of your array and is easily expanded as your data expands.

Just split the elements with Index Array, and wire each element into the Format Into String. You will need to either stretch the function to create additional inputs or you can just create the format string to add the inputs.

To create the format string manually, drop a string constant above the Format Into String function. Right-click and select \ formatting. Type the following into the string constant:

%d\t%.6e\t%.2f\r\n

This will give you an integer, a six digit exponential, and a 2 digit
floating point. Wire this string into the Format String input. The "edit format string" dialog will step you through the syntax for all the options.

The resulting string can be written to file directly with the Write Characters To File, or buffered by appending to the previous string and written all at once.

If your data is 2D, put a For Loop around the function and use a shift register to append the strings.

Michael
www.abcdefirm.com
Michael Munroe, CLD, CTD, MCP
Automate 1M+ VI Search, Sort and Edit operations with Property Inspector 5.1, now with a new Interactive Window Manager!
Now supports full project automation using one-click custom macros or CLI.
0 Kudos
Message 3 of 5
(3,566 Views)
Thanks... That worked... The data I wanted to be displayed with 6 digits of precision began at a certain (and known) column. All data before that column I wanted to display in no more than 2 digits of precision and all data from that column on I wanted displayed with 6 digits of precision. So I wired the 2-D array into a For Loop and then placed your NUMBER TO FRACTION STRING inside a True/False. As the For loop iterated, if the "i" iterator was less than a known value the True/False would then execute the true case which contained the number to fraction string with 2 wired to the digits of precision. For the false case I ran it through the number to fraction string with 6 wired to the digits of precision. I then let the output of the For Loop rebuild
the 2-D array. Last, I put it through a string to spreadsheet vi. I knew it had to be easy but I just didn't know which tool (in this case the Number to Fraction String) was the right way to go.. Thanks for the insight... bob paris...
0 Kudos
Message 4 of 5
(3,566 Views)
You could generalize it even further - have an array of precision specifiers, one for each column.

In your COLUMN loop, you auto-index out one of the specifiers, and wire that to a single NUMBER to FRACTION STRING converter.

The beauty of that particular function is that you can wire a 1-D array of numbers in, and get a 1-D array of strings out.
Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


LinkedIn

Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 5 of 5
(3,566 Views)