LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Read txt file and seach for numbers

Solved!
Go to solution

I have a text file which looks like this:

ref#1  2.1  3.0  -10

ref#2  3.3 -2.3  -1.5

 

I'd like the user to be able to:

A. key in a line number in the VI numeric control, and the numeric displays in the VI display the corresponding numbers in that line.

    e.g. If user key in "1", the three numeric displays show "2.1", "3.0" and "-10"

B. key in a string in the VI string control, and the numeric displays in the VI display the corresponding numbers in that line which contain the string

    e.g. If user key in "ref#2", the three numeric displays show "3.3", "2.3" and "-1.5"

 

Any sample code?

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

We cannot really tell what you text file looks like. What are the delimiters? (tabs? spaces?).

 

Here's what I would do (if the file is clean (one unique column delimiter, etc.)):

 

  • Read the file using read from spreadsheet file using the string polymorphic instance. This gives you a 2D array of strings.
  • Remove the first column and make it into a 1D array of strings.
  • Take the remaining numeric 2D array of strings and convert to an array of DBL.
  • Now wire the two arrays to a while loop so the user can interact (use a small wait or an event structure).
  • For scenario A, use index array on the numeric array to display the appropriate row.
  • For scenarion B, use search array on the string array, get the index, and proceed as above to get the row.

See how far you get ...

Message 2 of 7
(2,878 Views)

To get you started, I would read the file using the "Read from Spreadsheet file.vi" and delete off the first column.  You can then index off the row of numbers.  You can also use the Search 1D Array with the deleted column in order to search for you row index.  Give it a try from here.  If you need additional help, show us what you have tried and we'll get you there.

read text file.png


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 3 of 7
(2,872 Views)

I am actually programming VBAI (NI's Vision Builder for Automatic Inspection) and use LV's VI as custom user interface - which means VBAI would feed data to the VI.

One restriction of using VI with VBAI is that, dependencies are not allowed - which means in my VI I cannot have and subVIs.

Therefore the "Read from Spreadsheet file" subVI cannot be used in my case.

0 Kudos
Message 4 of 7
(2,869 Views)

Then read the entire file as a string and use the Spreadsheet string to Array primitive.


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 5 of 7
(2,867 Views)

I would actually recommend using the Run LabVIEW VI Step, which does allow for dependencies and then you can use all the Fil I/O and parsing functionality of LabVIEW. You can pass the line number requested in the Custom UI to the Run LabVIEW step and the Run LabVIEW step can return the three numbers, which a Update Custom UI step can use to update the values on your custom UI.

 

Hope this helps,

Brad

Message 6 of 7
(2,862 Views)

Oh yes this solved the dependencies problem 🙂

0 Kudos
Message 7 of 7
(2,828 Views)