NI Home > Community > NI Discussion Forums

DigiMetrix

Reply
Member
ZSH2010
Posts: 11
0 Kudos
Accepted Solution

How to read data from a text file?

How can I read numeric data from a text file?

Actually, I want to use a while loop and each time just read one array (one line) from the file. 

 

Then convert array to cluster. (I know this step, I used the function "array to cluster", right? )

 

Then assign this cluster to be the robot coordinates. (I connected the output of "array to cluster" to the "denso move by coordinates")

 

Anyone has any suggestions for me? Thanks! 

Active Participant
yaHeard
Posts: 238

Re: How to read data from a text file?

Hello,

 

You have quite a few options to do this, and the best solution really depends upon what you are wanting to accomplish.  Here are a couple basic options, though.

 

First, is it important that you read from the text file each iteration?  Could you simply read out the entire file's position data then cycle through that data later?  If so, you could just read the file once at the beginning and use a FOR loop to cycle through the points.  Here is a screenshot of an example:

 

ForLoop.png

This example reads a text file with the following data:

 

0,0,0,0,0,0,5

140,0,40,90,0,180,5

0,0,0,0,0,90,5

 

It will read out the data into a 2D array which can be fed directly into a FOR loop to cycle through each line of position data. The use of indexed tunnels allows the FOR loop to only iterate the number of dimensions of the array (number of rows in this case).  Note that when using the Array to Cluster function, you will need to specify the number of elements as 7.  It does not dynamically adjust.  If you must read from the text file each iteration, here is another simple example using a While Loop:

 

WhileLoop.png

Again, simple examples so you would want to make sure and check the robot status and do proper error handling for each method.  Is this close to what you were looking for?  I would also look into the File I/O examples that ship with LabVIEW for other options.
Carlton
CLD
Member
ZSH2010
Posts: 11
0 Kudos

Re: How to read data from a text file?

Thank you for your fast response! In first case, does the for loop automatically read one row each iteration? anything else I need to set?  

Active Participant
yaHeard
Posts: 238
0 Kudos

Re: How to read data from a text file?

Yes, since the position array's tunnel is set to be auto-indexed, it will cycle through each line of the 2D array each iteration.  When you connect an array to side of a FOR loop, it will be set to auto-indexed by default, so you do not have to change that.
Carlton
CLD