LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Change Formatting of Text File and Read back Number value for use in Waveform Graph

Solved!
Go to solution

Hi, I am working with LabVIEW 2018. I am using serial communication with a sensor that measure gas ppm in the air. I am writng the data I measure to a text file and I am having an issue with the formatting. I would like the file to write the data into columns labeled Date, Time, then have the sensor readings in the next column. I need them in this format to use in excel files and also to allow me to read back the values into my VI so that I can use them in Waveform graph. Currently, the VI reads a u16 value that cannot be used in a Waveform graph so I am using a waveform chart. Also, I would like to be able to reset the graph after the VI is stopped instead of manually resetting it. Any help or resources would be greatly appcricated. 

0 Kudos
Message 1 of 7
(1,230 Views)
Solution
Accepted by topic author vanceblake

To make a file readable by Excel you usually want to save it as a CSV or TSV file.  Comma-separated is a bit easier because Excel will open and self-format a ".csv" file automatically, while a TSV file has to be imported as an external file.  However, if you live in a locality that uses commas as a decimal separator then CSV can have additional issues with that, so a TSV might be better.

 

You probably want to change your format string to a constant instead of a control, and in the string just put commas between each element as you write it, plus a newline at the end.

 

In any case, to add headers to a file you should write those after you open the file but before entering the While loop, so they only write once.

 

As for the chart/graph, a chart is basically a graph that automatically manages certain things for you, but you probably want to manage it yourself.  Switch to a graph, and use your U16 as the Y value, and use the value of "Get date/time in seconds" as the X value.  Make sure to move that node inside of the While loop, by the way, so i you get an updated value each time.  You can then put an array containing all of the graph points into a shift register that initializes to an empty graph each time.

Message 2 of 7
(1,210 Views)

Hi Kyle, thank you for responding to my topic. I guess I was a bit unclear in my description. Sorry I'm still learning all the LabVIEW terminology. I just needed to have the functionality to open the file in Excel without formatting issues hence the .txt file type, and the desire for columns instead of rows. But I see what you mean with the other file types and will try them out. Could you explain what you mean by a shift register? Thanks for your help.

0 Kudos
Message 3 of 7
(1,179 Views)

If you wire something into or out of a While or For loop, you can right-click on the terminal and convert it into a shift register.  A shift register is a way to pass the output from a previous loop iteration into the current loop iteration.  The terminal looks like a little arrow pointing down on the left side and up on the right side.  You usually want to wire something into the initial terminal to set a starting value, though occasionally you don't need to if you want to get the value from the last time the VI ran that session.

 

For example, this code here uses a shift register to calculate factorials:

Kyle97330_0-1642008380536.png

You can see the first loop starts with the shift register getting set to 1, then each loop it multiplies the previous loop's value by 1, so you start with 1x1, then 1x2, then 2x3, then 6x4, then 24x5.

 

If you put the array you wire into the graph you use (instead of a chart) into the shift register, you can set it to an empty array on the start (so it clears the display), then add one point consisting of the next time stamp and measurement into the array, and then wire that new array into the shift register to pass it to the next loop iteration.

 

0 Kudos
Message 4 of 7
(1,171 Views)

Okay I see what you mean. I was reading the NI documentation about this as well, but I wasn't completely sure how it would affect the display. Now from my understanding of your idea, this method would be clearing the display after every loop? Cause now that Ive had some more time to think of what I trying to achieve, I would much rather have button that just clears the graph and operates independent of the while loop that is collecting data. Is there a way to do that?

0 Kudos
Message 5 of 7
(1,167 Views)

It wouldn't clear it after every loop if done right, it would update it each loop.  The first loop it would have 1 point, then second 2 points, and so on.  Each time you restart the VI it would go back to empty automatically if you put an initialized value into the input shift register.

0 Kudos
Message 6 of 7
(1,154 Views)

Okay I wil try to implement it. Thanks for all your help.

0 Kudos
Message 7 of 7
(1,124 Views)