From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Getting the Last Number in a data file, the best way to do it

Hi,

I have a data file which has a list of numbers, each on a seperate line. I need to get the last number in the file and was wondering how to best go about doing this. I tried using readFromSpreadsheetFIle but this does not seem to work. Thanks for any help.
Intern NSWCCD Carderock.
0 Kudos
Message 1 of 11
(3,910 Views)
Read From Spreadsheet File ought to work.

Get the file data as a spreadsheet (2-D array of numbers).

Use ARRAY LENGTH to find out how many rows and columns you have.

Pick out the number of ROWS.

Remember that the rows are numbered 0.. N-1

Use INDEX ARRAY to pick out column 0 of row N-1 : that's your last number.


If you wanted to use less space, do it yourself with a WHILE loop:

Read a line and put it into a shift register.

When you hit the EOF (end of file), stop the loop and use the value in your shift reg as the last number read.
Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


Blog for (mostly LabVIEW) programmers: Tips And Tricks

Message 2 of 11
(3,899 Views)
If this is a very large file it would be inefficient to read and parse the entire file just to get the last number. Read the last 20 (or so) characters with "Read characters from file" where the "start of read offset" is wired as e.g. "file size-20". Now it should be easy to get the number you want with some simple string scanning.
0 Kudos
Message 3 of 11
(3,885 Views)
here is a simple vi that, assuming the data structure is a 1D array, will find the last element.
Message 4 of 11
(3,879 Views)
Here's one possibility that I mentioned. It will be much more efficient that the other suggested solutions if your files are big. It does not make sense to read megabytes of data, convert it all to a huge array that needs to be allocated in memory, then clip off it's tail and throw away the rest! 😉

All you need is to read the last few characters of the file....

Message Edited by altenbach on 06-02-2005 06:23 PM

0 Kudos
Message 5 of 11
(3,877 Views)
Thanks for the help.
Intern NSWCCD Carderock.
0 Kudos
Message 6 of 11
(3,868 Views)
If you are creating these files, you really should consider a binary format. ASCII is ill suited to large files, as you have just discovered. If you are using waveforms, try NI-HWS, available on the driver CD. It fully supports fetching subsets.
Message 7 of 11
(3,856 Views)
Sorry to bring up such an old thread, but this is pretty much the problem I am trying to solve. In the VI I am writing, I am receiving a constant hex stream from COM5 on my desktop and doing some number manipulation to pick out and convert part of the hex stream to a Watt-Second number. I am than taking this number, every second, and dumping it to a text file for later use. Well, now I need to take the last two Watt-Second readings from the text file and subract the first from the second to get my power consumption. However, my problem lies within picking the last two measurements off the file. Even if I could get it so there were only two variables instead of writing to a text file, I could live with that, I just don't know how to do it. Also, is it even possible to do what I want to do, i.e. write and read from a text file within a second? Or would it have to be a longer gap or would there be file access issues?

I would like to try the VI screenshot that altenbach posted, but I have never seen those file icons on the left and can't find them in Labview.

I've also attached the VI I am working on, to avoid any confusion, the For loop where the data is being written to the file was my attempt at trying to get a row count for how many times a value was written to the file.


Any help would be greatly appreciated. Thank you!
0 Kudos
Message 8 of 11
(3,474 Views)
Your VI is a bit of a mess. First of all, you can only get a single reading unless you use the run continuous button and the run continuous button should not be used as a normal way of running any VI. You don't want to be calling the VISA configure Serial Port, VISA Clear, and Close Serial port all of the time. What you want to do is place a while loop around your acquisition and file write code. The other VISA functions belong outside the loop. With a while loop, you can use a couple of shift registers to keep the last two values read. No need to try and do any reading of the file. Get rid of the duplicate VISA Configure Serial Port and VISA Close. It will only cause problems. Put your VISA Write after the VISA Read. Dataflow will take of all sequencing.
0 Kudos
Message 9 of 11
(3,453 Views)
Thank you Dennis! Your shift register solution worked (as I'm sure you knew it would). Also, thank you for the tips on using While loops instead of running continuously, I never knew it was bad practice to do that.
0 Kudos
Message 10 of 11
(3,412 Views)