LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Having trouble writing data to xlsx file

Solved!
Go to solution

Good evening Everyone,

 

I'm hoping for a little assistance. For my class I wrote a random number generator program, which works. In addition, I created a 2D array where the first row of 20 elements represents the random number created and the second row represents on which iteration the random number was created. With the 2D created in the labview, I'm now trying to write the data to an xlsx file. Currently, I'm using the write delimited spreadsheet node to achieve this goal, but without any success. I have attached my code in zip file.

 

Thanks in advance, pat!

0 Kudos
Message 1 of 10
(2,887 Views)
Solution
Accepted by topic author EngineerPat

Read the Help for Write Delimited Spreadsheet.  It writes a text file, with columns delimited by (in your case,) commas, and rows delimited by New Lines (<CR><LF>).  This is called a "Comma-Separated Values" (or .csv) file.  You gave it an extension .xlsx, which (if you have Excel installed) Windows will try to send to Excel, although it is not what Excel expects as its proprietary file type, and Excel will (probably) throw an error.  Try opening the file with NotePad or any other "text-only" routine -- you should see a perfectly nice text file.

 

Bob Schor

Message 2 of 10
(2,861 Views)

Hi Pat,

 

As Bob_Schor mentioned, you're telling Excel that you're writing a .xlsx file, when you're not.

 

If you really want to write an Excel file, you can look at the Report Generation Toolkit. However, if this isn't a strict requirement (and I'd guess it usually isn't!) you can instead just change the path to write a "Random_Number_Data.csv" file. Excel will happily open this. You can also reorder your array or transpose as necessary to get the 'shape' you'd like in your csv file (->Excel).

 

As a further note, if you'd like the Graph to update in real time (that is, during the execution of your generation) you'll want to put the graph (or chart) inside the loop.

In this specific case, a Chart will likely be much simpler to program (because a chart appends data up to a "History Length", configurable programatically or more simply by right clicking on the chart. You can then just connect the random number to the Waveform Chart.

For a Graph, you'd want to take a look at Shift Registers probably!


GCentral
Message 3 of 10
(2,856 Views)

@Bob_Schor wrote:

Read the Help for Write Delimited Spreadsheet.  It writes a text file, with columns delimited by (in your case,) commas, and rows delimited by New Lines (<CR><LF>).  This is called a "Comma-Separated Values" (or .csv) file.


My personal preference is to use the tab (default) delimited format with a txt extension.  Excel still opens these up just fine.  I find the tabs easier to read in a text editor and it also works better internationally (there are countries that use the comma as the decimal separator).


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 4 of 10
(2,815 Views)

Good morning Bob,

 

Thank you for the quick reply. I did some more research with the detail help and was to get the program to work. I appreciate you pointing me in the right direction. I may have another question but I'm going to give it a whirl before I ask. Once again thanks for the assistance.

0 Kudos
Message 5 of 10
(2,804 Views)

Hi cbutcher,

 

I would love to use a chart for this situation but my professor is requesting us to use a waveform graph. I appreciate the explanation on how to implement a chart for this situation, I did know how to get a chart to update in real time. I'm still playing with transpose function and trying to understand the function better. Thank you for the help!

0 Kudos
Message 6 of 10
(2,800 Views)

Good evening everyone!

 

Before I dive into my questions, I'll provide some background regarding the code. I have been tasked with developing a random number generator that generates 20 values that will be written to a csv file. In addition, I must provide an input for users that allows them whether or not if they want to generate number random values or recall values from the csv file. This is where the write/read delimited spreadsheet nodes come into play. I have a few questions regarding the Write and Read delimited spreadsheet nodes. In regard to the write node, my data always starts in the second row of my csv file. Why is the data entry starting on the second row of the csv file?  This is a problem because when I read the file, I get a row of zeros output in my 2D (it should be the random numbers).  Also, my waveform graph comes out looking crazy when reading from the csv file, is there anyway I can clean this up? I have attached my logic and csv file in a zip folder. Thanks for the help everyone.

0 Kudos
Message 7 of 10
(2,786 Views)
Solution
Accepted by topic author EngineerPat

A few thoughts (numbers for reference, not importance):

  1. You have a While loop where the termination condition is "=19" from the last value of the "i" terminal from the For loop. This is always true if you have 20 iterations, and never true if you change this (20 iterations). As a result, the While loop does nothing for you (except add confusion).
  2. To write a graph during a loop, use a Shift Register as I mentioned. A snippet shows this below.
  3. If you want to use the same path in two different cases, place the value outside (before) the case structure. This simplifies changing it in future (or if you download someone else's code and you don't have the path they specified 😉  )
    1. (3b) if you wanted to specify a path relative to some system-defined directory, Get System Directory might be interesting for you
  4. When I tested your VI (with minor modifications as mentioned above regarding path) I didn't have any problems related to writing or reading. In particular, I didn't see a row of zeros in the csv file or when reading back the array. An image of the values when read back is also inlined below

Graph 2 updates during the generation. The values can be found on the outgoing shift register terminal for storing.Graph 2 updates during the generation. The values can be found on the outgoing shift register terminal for storing.

Read back of stored values from .csv fileRead back of stored values from .csv file

 

Note that when you append to an existing file, they will appear below the previous entries. With this layout, that means you'll plot them as multiple plots like this:

Multiple plots read back in. Note the overlay of plots 1 and 3 (they have identical data). Here I made plot 3 use points to display this.Multiple plots read back in. Note the overlay of plots 1 and 3 (they have identical data). Here I made plot 3 use points to display this.

If that's not what you want, you might need to consider transposing the data.

 


GCentral
Message 8 of 10
(2,755 Views)

Hi cbutcher,

 

Thank you for such a detail response. I'm trying to go through the logic and truly understand your modifications and the advice you offered. I do have one question, is there no logic in the FALSE state of the case structure? 

 

Thank you for the help - Pat

0 Kudos
Message 9 of 10
(2,729 Views)

The false case is the same as the one you uploaded, just the Read from the file. I only changed the input path to be shared with the True case.


GCentral
0 Kudos
Message 10 of 10
(2,721 Views)