NI Home
Cart Cart | Help
Hello Events Academic NI Developer Zone Support Solutions Products & Services Contact NI MyNI
You are here: 
NI Home > NI Developer Zone > NI Discussion Forums


Reply
Member
splee
Posts: 134
0 Kudos
Accepted Solution

Read txt file and seach for numbers

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?

Knight of NI
altenbach
Posts: 23,141

Re: Read txt file and seach for numbers

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 ...


LabVIEW Champion . Do more with less code and in less time .

Trusted Enthusiast
crossrulz
Posts: 4,024

Re: Read txt file and seach for numbers

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

Member
splee
Posts: 134
0 Kudos

Re: Read txt file and seach for numbers

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.

Trusted Enthusiast
crossrulz
Posts: 4,024

Re: Read txt file and seach for numbers

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

Active Participant
Brad
Posts: 600

Re: Read txt file and seach for numbers

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

Member
splee
Posts: 134
0 Kudos

Re: Read txt file and seach for numbers

Oh yes this solved the dependencies problem :smileyhappy:

By using this web site, you accept the Terms of Use for this web site. Please read these Terms of Use carefully before using any part of this site. Please go here for information on ni.com's copyright infringement policy.
My Profile | Privacy | Legal | Contact NI © 2011 National Instruments Corporation. All rights reserved.    |    E-Mail this Page E-Mail this Page