LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Save columns/rows into file with headers

Hello there,

 

I am trying to save data into a text or spreadsheet file, with headers for each column.

 

My data needs to be entered as an entire row or column, and any subsequent row/column needs to be appended as per user request. I have made a graphical descriptor:

 

LV_issue.png

 

I want to create a file, write the headers, write data set 1, save/close the file, reopen when requested, add data set 2, save/close file, etc.

 

I've tried read/write text file, read/write spreadsheet file, but none seem to work without adding data row by row. Any help is welcome.

 

Ben

0 Kudos
Message 1 of 14
(8,817 Views)

You don't need to Read and then use Replace Array Subset.

 

To start with, Build Array more clearly carries out the operation you want here (append to array), but there's no need to do that either because you can wire the new results to the 1D array input of the Write (outside of the case structure) and just set the optional boolean to True for Append Data (by default it is false).


GCentral
0 Kudos
Message 2 of 14
(8,792 Views)

Here are two things you might try, depending on what Toolkits you have available for your LabVIEW 2013 installation.

  1. Use the Report Generation Toolkit to save your data in an Excel Worksheet.  The Excel Easy Table function allows you to write a Table (formatted as a 2D array of N rows and 1 column) with a Header, and add it to the right of existing columns (there may be some work you'd need to do to determine how many columns you have -- the easiest is just to read all the data in and determine the Array Size).  This saves the data as a true .xlsx file, so you need to use the RGT to read it back.
  2. Use Read Delimited Spreadsheet/Write Delimited Spreadsheet to read the Header Row and then read the Data Array.  Add to the 1D Header Row the new Column's Header, and append to the 2D Array the next Column's Data.  Then use Write Delimited Spreadsheet to write the new Header, then write the new Data.

Now that I've stated it this way, Option 2 (which doesn't need the Toolkit) seems like a piece of cake.  Note that you'll get Text files (not .xlsx files) with this latter method.

 

Bob Schor

0 Kudos
Message 3 of 14
(8,780 Views)

Thank you for the suggestion cbutcher but it doesn't work. It will append the new array to column 1. I can't get it to append under column 2, 3, 4... etc

 

My issue is I can't specifically request it to append to my chosen column.

0 Kudos
Message 4 of 14
(8,773 Views)

Hi Bob,

 

Thank you for your reply.

 

Are you suggesting I write the header for each column sequentially while simultaneously writing the data underneath it by appending? Therefore each iteration adds the next column header and its respective data?

 

So in this case I think I would have to read the relevant value of the 1D header array, incrementing through the array by 1 each time it is called/accessed. I would need to extract the row size of the main file, use that as the index for the string array, and then add the data underneath it.

 

Text files are no problem, anything Excel can read is good for me.

 

Ben

0 Kudos
Message 5 of 14
(8,774 Views)

I found that if I just appended the 1D array data to the header, in subsequent writes it replaced the string header data with 0. So I started as a string spreadsheet and converted everything to strings. It now works as intended.

I have attached the complete vi for anyone who is interested.

 

Please note, this is NOT the way I wanted the code to work. In some ways I think it is a poor hack.

I wanted to generate all the string headers at the beginning, a full row of headers, and add each 1D array to each column in turn, underneath its respective header. But unfortunately there is no good way to do that in labview.

 

There seems to be no concise function that allows: "Insert this 1D array into this 2D array starting at this position in the 2D array"

 

Ben

0 Kudos
Message 6 of 14
(8,754 Views)

If you don't mind using a csv file, then you can use Write Text File to do what you want. It still requires string inputs, but that's effectively all you get with a text file...

 

There's a function in the string palette that allows you to set the file position - you can set relative to the end of the file to move the position to the end (I.e. append). You need to do this every time you open the file reference if you go this route. 


GCentral
0 Kudos
Message 7 of 14
(8,747 Views)

Capture.JPGYour text file name is "test6(3).txt" so I am assuming that it is NOT an excel file. Rather a comma or tab delimited text file.

 

I cleaned up your VI:

You don't need the stacked sequence with the event structure. Just make the boolean's mechanical action to be "Latched when Released" and put each control in its respective event.

 

You don't need the timeout event. You don't do anything there.

 

Add a "STOP" event.

 

I replaced the deprecated Write to Spreadsheet File.

 

You don't need to read the old data->append the new data to that->then rewrite all the data to the file. Just set the boolean to append the data to the file.

 

write log file.png

0 Kudos
Message 8 of 14
(8,733 Views)

Hi jamiva,

 

Thank you for your reply!

 

I am out of office/lab for a few days but I will clean it up when I get back. I can't tell exactly from your image, but the last time I used a simple append to add the next data set to the file it just appended it on the end of the first column. I couldn't get it to add the second set of generated voltages underneath the second header (e.g. T1 UP).

 

Unless I'm misunderstanding what the new "write to" symbol does (I'm not familiar with it by eye), it looks like that will give you one long column of data?

 

Ben

0 Kudos
Message 9 of 14
(8,724 Views)

Hi jamiva,

 

Could you possibly attach the .vi you made in your example?

 

Thank you

0 Kudos
Message 10 of 14
(8,682 Views)