LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Case Structure timing/ writing data to a excel

Hi all,

 

I have written a program that has the main functions of allowing the user to generate a random number between either 0-10 or 5-15 by using the labelled switch. I also have a control which can switch this function on or off. When this switch is true it will display the random number and also write this number to an excel spreadsheet every 1 second (I hope). When false this will stop displaying the number and hence not write the number to a file.

 

However, I am having the following issues:

 

1. Excel file is corrupt once I begin to write the generated numbers to the spreadsheet. (I am trying to write to a .xlsx file)

2. Once I begin to try write the numbers to file, the elapsed timer only displays the time is 1second intervals rather than the full time.

 

I changed the .xlsx file to a .csv file and this solved issue 1. but it only recorded one random number rather than the numbers that were generated in that time since switched on/off.

 

Should I have the write to spreadsheet function in the timed case structure?

Should the file path control be outside this case structure?

Have I set up the write to spreadsheet VI correctly?

 

 

If anyone could help with these issues that would be a big help.

 

Thanks!

 

 

 

 

0 Kudos
Message 1 of 14
(4,200 Views)

Hi,

I'm not sure I understood your problems correctly here, but I think you only have to write a TRUE constant to "append to file? (new file:F)" input of the "Write To Spreadsheet File.vi". In this case the generated values will be added to the file instead of replacing it. I also suggest to use a wait function in the false case of the upper case structure to avoid using too much CPU time while "Generate random number and display" control is set to FALSE.

Lucian
CLA
Message 2 of 14
(4,184 Views)

You have multiple problems.

 

1.  Your path control is set to only select Existing files.  You can't type a new file name in there.  Change the Browse Options to be "New or Existing".

2.  As LucianM said, you should wire a True constant to the append of the "Write to Spreadsheet File" function.  Without it, your new value overwrites the existing file.

 

3.  Most importantly.  YOU ARE NOT WRITING TO AN EXCEL FILE.  Write to Spreadsheet File does not create Excel files, which is a proprietary file format.  It creates a delimited text file.  Now Excel can import delimited text files, but the file itself is not an actual Excel file.  You say you are trying to write to an .xlsx file.  Don't name your file that because it is not an .xlsx file.  Call it .txt, or .csv. or something else.  Excel says you have a corrupted file because when it tries to open the file with an .xlsx extension, it expects the file to be structured in a certain way (as an XML file), but the file does not contain any of that information.

Message 3 of 14
(4,166 Views)

Thank you both for the advice and help.

 

I have made the changes you both suggested and the data writes to the file as required.

 

There is still an issue with the elapsed time. Once I begin I turn the top boolean control to true, the elapsed time begins changing at the set time increment inside the case structure. Is there anyway to stop this from happening?

0 Kudos
Message 4 of 14
(4,161 Views)
0 Kudos
Message 5 of 14
(4,159 Views)

The 5000 millisecond Wait until Next in the case structure is executing and that is what controls the pace of your while loop.

 

You could put your elapsed time counter in a separate while loop.

 

You could use the Time Has Elapsed output of that timer, AND it with the control you use to generate the number so that the entire loop runs faster, but the value is only written when the button is pressed AND the time has elapased.

0 Kudos
Message 6 of 14
(4,144 Views)

To prevent the Wait until Next function from controlling the while loop I have seperated it as shown in the attatchment.

 

However, it has stopped generating the random numbers. Is it OK to pass the values through the while loops like I have done? Or is there another error in doing this?

0 Kudos
Message 7 of 14
(4,124 Views)

@lneill65 wrote:

There is still an issue with the elapsed time. Once I begin I turn the top boolean control to true, the elapsed time begins changing at the set time increment inside the case structure. Is there anyway to stop this from happening?


Your elapsed time display is independent of the boolean switches because it is on the toplevel loop diagram. I am not sure why you place a five second wait inside the case structure if you want to write once per second.

I am not sure why you have two sequential case structures since the calculations of the first structure is not needed unless the other case is also true. Why not place it inside the true case?

 

You should also adhere to some basic coding styles. Avoid overlapping and floating objects, hidden wires, right-to-left wiring.

 

If it is sufficient if the code reacts once per second, just run the loop with a 1 second wait. If you don't want to display a stale random number if the switch is off, place the indicator after the case structure.

 

Here's a quick draft. Modify as needed. I probably would use an event structure, but let's keep it simple at this stage. You should also modify it so it does not try to write with an illegal path and also retain the selected path once selected. I propably would use a file dialog instead of a FP Path control.

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

@lneill65 wrote:

To prevent the Wait until Next function from controlling the while loop I have seperated it as shown in the attatchment.


You should palce a small wait inside that loop to prevent it from spinning millions of times per second.


@lneill65 wrote:

However, it has stopped generating the random numbers. Is it OK to pass the values through the while loops like I have done? Or is there another error in doing this?


You have a basic misunderstanding of dataflow and I would highly recommend to do some tutorials first. Since the second loop depends on data from the frist loop, it can only start executing once the first loop has completed. Study my above example for a better alternative.

 

0 Kudos
Message 9 of 14
(4,110 Views)

Hi altenbach,

 

Thanks for the help!

 

As you probably guessed I am very new to LabVIEW. I was trying to use the program I supplied to test whether I could apply a similar approach to the program I have been writing to conduct an experiment (I haven't got the hardware needed yet).

 

Basically this experiment calculates the height of a sensor and outputs this result as an indicator on the front panel. However I have tried to implement a piece of program which will write this height to an excel file at 1 second intervals (Switch to tell it to record or not record).

 

I am trying to avoid the Wait Until ms from controling the main loop.

Do you think this will work or should I seperate the loops rather than using the nested loop inside the main?

0 Kudos
Message 10 of 14
(4,100 Views)