LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Problems with opening file with while loop

Solved!
Go to solution

Greetings everyone,

 

  This is probably a quick question, but I am familiarizing myself how to open data files in Labview. The attached code works if you enter a file from the folder button on the front panel. However, if the path is blank and you hit the run (arrow) button, a dialog box comes up and asks for the file.

 

   I select the file, but the dialog box keeps coming up. I think this has something to do with my while loop. Can anyone tell me where I am going wrong?

 

  Thanks!

   TheLT

0 Kudos
Message 1 of 23
(4,539 Views)

I don't have LabVIEW on this machine, but here's my guess based on your description.

 

You should only open and close the file once.  Those two functions should be outside of the while loop.  You can then read and/or write as much as you want inside of the loop.


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 2 of 23
(4,535 Views)

crossrulz,

 

  Thanks for the response, but I don't have a open or close operation in there. I am using Read from Spreadsheet.

 

   Sorry for the lack of detail.

 

   Thanks!

    TheLT

0 Kudos
Message 3 of 23
(4,528 Views)

You do have an open and close operation in your loop, you just don't see it because it's inside the read from spreadsheet file VISmiley Wink You need to pull the read from spreadsheet file out from being inside the loop, or store the path in a shift register after the user selects it. The problem is it keeps passing in an empty path because you don't save the user's selected path, so the initial value (empty path) is always being used.

 

I would recommend reading the file once though, outside the loop. Even if you store the path in a shift register, you will still be opening and closing the file in the background repeatedly until the loop exits.

 

Also, look into state machines. You really don't need two while loops in two different cases of a stacked sequence structure to accomplish what you are trying to do.

Message 4 of 23
(4,521 Views)

So if I did that, how can I get down past the header information, as this is what the while loop does?

 

I can send the data file as well if needed.

 

Thanks!

TheLT

0 Kudos
Message 5 of 23
(4,518 Views)

This is much simpler, read the whole file once, find the end of the header, then get a subset that includes the header info. The functions are "index array", "search 1d array", and array subset (except I noticed my subset is wrong. Wire the found index from teh search 1d array into the length of the array subset, not the index of array subset.

 

There's no reason to read the file more than once, the information in it isn't changing. Just continue reusing the array that comes out of the file after reading it one time. I'm also not sure if you want to index the row or column when finding the end of the header since I don't have the data file, but I did my best from your existing code. You should really be able to modify this as needed.

 

Message 6 of 23
(4,515 Views)

TheLT,

 

1. crossrulz was right. The Read FromSpreadsheet File.vi opens and closes the file each time it is called. If no file path is present at the input when it is called, it will pop up a file dialog.

2. LabVIEW uses a dataflow paradigm. In your program the effect of this is that the File path control is read almost immediately when the program is started and never again after that. So, if the program is already running when the file is selected in that control, the new value is never read and the value (empty) in the control when the rpogram started is used.

3. The fix is to use an event structure with a file path Value Changed event to detect when the control has a value entered and then read the file.

4. Sequence structures obscure code, make it difficult to modify and extend code, and can almost always be eliminated by effective use of dataflow.  Local variables should not be used just to pass data around.  Wire is always the best way when possible. In your program adding a few wires allows elimination of the sequence structure and the local variables.

5.  Your graph loop should have some kind of delay or wait.  No need for it to run as fast as the processor will allow - and to hog all the CPU time - to repeatedly put the same data onto the graph.  This is another place where an event structure is appropriate. Only update the graph when the X-Axis or Y-Axis selections have changed. Note: Accepted best practice is to use only one event structure in a program, although there are a few advanced cases where multiple event structures are appropriate.  You only need one.

6. If the slected file does not contain the "endheader" string, the program will never stop.  Add a test for EOF to stop the program if the End of File is reached without finding the flag.

 

Lynn

 

Message 7 of 23
(4,508 Views)

Of course Greg and Lynn are absolutely correct ant there are many more simplifications possible.

 

Can you attach a typical datafile for testing?

 

Use gregs code to read the file, write all the static properties, then use a small event loop listening for value changes in the in the two rings.

Message 8 of 23
(4,485 Views)

All,

 

 

  Thanks for the reponses. The data file is attached. Does that change any of your responses?

 

   Thanks!

   TheLT

0 Kudos
Message 9 of 23
(4,459 Views)

Greg,

 

  I did edit my code to match yours as follows (attached, along with the data file again just in case), but I did notice two things:

 

  1.) It still prompts for my file more than once. Now I do have the file path wired into Read from Spreadsheet.vi, but I even tried to unwire it, does the same thing.

  2.) I also notice that my list boxes aren't populated. Any suggestions on that?

 

 

 

  Thanks!
   TheLT

Download All
0 Kudos
Message 10 of 23
(4,423 Views)