Showing results for 
Search instead for 
Did you mean: 

Add header to output file

Go to solution

Hi, I am experimenting with event driven programming. My goal is to retrieve data from a wavelength meter, with option to view it or to view and record. When recording I would like to save to either text or spreadsheet file with header information for Wavelength in first column and Power in second.


Currently I've got random numbers for mock data that is concatenated then spreadsheet string to array which is then written to delimited spreadsheet. It should record until stop case (button click) is called.


I'm having a hard time figuring out how to add header to top of data. Any code suggestions are appreciated (particularly code from random numbers to file write, I think this is not done best).



0 Kudos
Message 1 of 10

Put another write to file function write before your current one.  Write a 1-D string of arrays.

0 Kudos
Message 2 of 10

Thanks for suggestion! I did an extra write but the header text got turned into zeros. Probably formatting changed but not sure how to fix it. See new code:

0 Kudos
Message 3 of 10

The format into String is converting the data into doubles.  Wire a string constant into that Format into string to set the type.  Or better yet, just get rid of it completely and wire in a string constant of header1,header2,header3\n  (where this is set in \code mode with the display style turned on so \n is read as a new line character.


Then you need to wire the two functions in series taking the error wire from the first to the input of the second, and the file path wire from the output of the first to the input of the second, since right now they are in parallel and either one could execute first.

0 Kudos
Message 4 of 10

I like the simplicity of just adding a string constant to the write delimited. But it only takes 1d or 2d data, which is numerical. How would I add text data?

0 Kudos
Message 5 of 10

In that case, just use a build array to turn the string into a 1 element 1-D array.  (Now that you mention it, you might not need to put the \n at the end of your string as the text/spreadsheet file function might to it for you automatically.)

0 Kudos
Message 6 of 10

Thanks for the continued help!

I got the header created, but it creates additional headers each time the code records. What is best way to get it to only write one time? I've seen it done in loops, but that might not be best for this code.

0 Kudos
Message 7 of 10
Accepted by topic author Triplett

It looks like you are creating a new file every time that case runs because you are building a filename based on the timestamp.


Or it is just creating a new file each time the minute changes?


What you can do is put the part that creates the header into a case structure that only executes when the filename is not equal to the previous filename.



I see you did not fix the file path through the first function like I said.  And it looks like your doing multiple string manipulations to get from the data to the file which is pure Rube Goldberg  (1.  number to string.  2, string to array to number.  3.  number to file which is a string based on the format code in the file function.


See the modifications that greatly simplify that code.  You'll have to make sure it is doing what you want because your original code is so unnecessarily complicated, it might actually be doing something different than what it seems like it should.

Message 8 of 10

Thanks, This helps clear things up. I need to learn about feedback nodes, that is really useful!

0 Kudos
Message 9 of 10

Sorry, I started this, then got called away, and missed the intervening posts.  But it still might be helpful ...


This is actually "pretty close".  Let me simplify the task a little bit and see if we can get over a few more hurdles.  Let's consider the following task, with only two buttons, Record and Quit (both Square "Latching" buttons), and a State Machine with only three States:  Idle, Recording, and Done.  We'll keep the Event Structure, and consider three Events: TimeOut, Record (Value Change), and Quit (Value Change).


Let's initialize the State Machine to Idle, and consider what to do in the three States:

  • Idle -- Do nothing, but be receptive to a Button Press.
  • Recording -- Take a Data Point every N Milliseconds and write it to an already-opened Delimited Spreadsheet File.
  • Done -- close Delimited Spreadsheet File and stop the program.

Now, what do we want to do when the Buttons are pressed?

  • Record -- Assuming (?) we are in the Idle State, do the following:
    • Open the Delimited Spreadsheet File.
    • Write the Header.
    • Change State to Recording.
    • Arrange for a Loop to run every N milliseconds.
    • Prevent another press of the Recording Button.
  • Quit -- Close Delimited Spreadsheet File and Exit the Program.

Oops -- the Quit button does the same thing as the Done State, so maybe we don't need the Done State!


Notice that we've reduced the Timing requirement to the Recording State -- the Idle State doesn't require timing.  Given your current Block Diagram, can you think of a way to have the Timing initially "off", and then set to N Milliseconds when the Record button is pressed?  [Hint -- think Shift Register].


Bob Schor



0 Kudos
Message 10 of 10