12-03-2014 05:36 AM
3 days I have now spent on what seems like a simple problem:
I have a VI that reads from a TDMS file of significant length, containing data sampled at 1 MHz on 2 channels (very large files obviously). The VI scans through 1 second at a time and searches for any threshold-crossing points, and calculates the energy at that point. Anyway, I'm trying to write a text file where one column contains the sample number and the second column contains the energy, appended each time the VI iterates (the number of rows generated per iteration will vary). This is simple using the 'Write to Spreasheet' function, the problem is I want different formatting on each column. The sample number should be set to zero decimal places and the energy should be set to, lets say, 3 significant figures.
A typical few lines should look like (tab delimited, the commas are only there to represent tabs):
Time (samples), Energy
267935, 0.0000346
545227, 0.000298
1298655, 0.0000000122
1314522, 0.00854
'Write to Spreasheet' allows only one format, so I either have a huge number of DPs on my sample number or the energy value is 0.
If I use 'Array to Spreadsheet String' I can set the formatting, however I just can't seem to concatenate, build or transpose the outputs in the correct way no matter what I try.
In the attached image I have been using Ch1 as my experiment; Ch2 uses the traditional but unsatisfactory 'Write to Spreadsheet' method. The current VI returns on Ch1:
Time (samples), Energy
267935
545227
0.0000346
0.000298
1298655
1314522
0.0000000122
0.00854
I hope the image of the relevant portion of the VI is enough to go on. I can attach the VI if necessary but it seems pointless as I won't be able to share any of the data files for testing.
Solved! Go to Solution.
12-03-2014 05:43 AM - edited 12-03-2014 05:54 AM
12-03-2014 05:50 AM
You can use the "Number To Fractional String" primitive to convert the DBL data to string along with the required precesion. Then when you use this 2D string array and write it to the file using "Write to Spreadsheet VI" it should work fine.
If you still face any issue please attach the code with the data in a constant and saved in 2012 version.
12-03-2014 06:09 AM
Thanks for your replies.
GerdW:
I was converting to 2D arrays because the 1D array generates a row in the text file. I want to build columns but the transpose function doesn't work with string inputs, therefore I assumed my only option was to transpose before converting to string... but I have no clue whether this was the right approach. I would be interested to know in what order you would wire functions to generate the 2D array after the formatting?
P@Anand:
Thank you, this is closer than anything I have managed so far and near enough to be a solution. It's still not quite perfect as I can only specify precision (DPs) rather than significant figures, but it certainly looks a lot tidier in the text file.
12-03-2014 06:19 AM
@zaq wrote:
[...]rather than significant figures, but it certainly looks a lot tidier in the text file.
You have seen the optional input "width"? If not, try it.
Also, you might want to lookfor "Format into String" as you can specify the width, precious and padding setting.
Norbert
12-03-2014 06:35 AM
12-03-2014 08:05 AM - edited 12-03-2014 08:07 AM
It was my understanding that the 'width' option of 'Number to Fractional String' padded spaces to the left to make up the character length, rather than having any effect on significant figures? I haven't been able to produce 3 SF with any combination of width and precision I have tried so far.
GerdW:
Thanks again, that also works albeit with the same issue as above. I think my problem was that I was using 'Array to Spreadsheet String', which gave me a broken wire when I tried to transpose (both with 1D and 2D arrays). 'Number to Fractional String' was the subVI I should have been using, the output of which can be transposed.
12-03-2014 08:16 AM - edited 12-03-2014 08:19 AM
Ok, I have managed to get this working as I wanted using 'Format into String' and the method shown in the attached image.
Cheers for your help.