LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Build array & write to text with different column formatting

Solved!
Go to solution

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.

0 Kudos
Message 1 of 8
(5,028 Views)

Why do you create 2D arrays when you only have 1D arrays of data?

 

Stick with 1D arrays for the formatting functions! After you have done your formatting you can easily build a 2D array from your columns of data…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 8
(5,020 Views)
Solution
Accepted by topic author zaq

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.

-----

The best solution is the one you find it by yourself
Message 3 of 8
(5,017 Views)

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.

0 Kudos
Message 4 of 8
(5,004 Views)

@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

 

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
0 Kudos
Message 5 of 8
(4,996 Views)

Hi zaq,

 

first format your columns, then create 2D array from them:

check.png

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 6 of 8
(4,988 Views)

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.

0 Kudos
Message 7 of 8
(4,970 Views)

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.

0 Kudos
Message 8 of 8
(4,962 Views)