LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Reading data from Text file and accessing Lines

Okay, here's what I want to do. I have a text file that looks like this:

1,2,3
4,5,6
7,8,9

and I need to access the 1, the 2, and the 3 as three separate values then I need 4, then 5, then 6, etc. Each of the 3 values represents a particular input to a larger VI but the input is coming from a text file. So I need to access 3 values per line and I need the line to change (i.e. go from line 1, then line 2, etc).

I know this can't be as difficult as I'm making it. Can I get some help?

Thanks in advance,

Michael
0 Kudos
Message 1 of 12
(7,059 Views)
How about some "classic" ANSI C?
 
--------------------------
FILE *f;
int val1, val2, val3;
 
f = fopen ("filename.txt","r");
 
while (!feof(f))
{
    fscanf(f,"%d,%d,%d\n", &val1, &val2, &val3);
 
   SetCtrlVal (panel,  control,  val1);
   ....etc.....
}
fclose(f);
-----------------------------

The end of file check may not be exactly what you want, but the general idea is there.
 
0 Kudos
Message 2 of 12
(7,053 Views)
🙂 Thanks but I want to do it in LabVIEW exclusively. Can you tell me how to do it strictly in LV?

0 Kudos
Message 3 of 12
(7,052 Views)

To read file contents one line at a time you can use Read lines from file.vi in the File palette (possibly in the Advanced sub-palette, I don't remember wxactly and I cannot check at the moment) . Once read, the line could be separated into its components using the Scan string for tokens.vi. In the online reference for the latter two examples are listed that you may want to study.

Last but not least, for LabVIEW related questions I suggest you to post them directly to the LabVIEW forum, since this board is dedicated to LabWindows/CVI: you will find more rapid response there Smiley Happy



Message Edited by Roberto Bozzolo on 03-14-2008 12:03 AM


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 12
(7,046 Views)

hi ,

 

i want to read data from  file  , so i have to use fscanf  command to read it , but problem is i can not  to do it .

can you give me the exact  syntax of fscanf ....i mean i dont know how to use it  in my particular case. which is very simple. :(.

data  which i have to read is .

8.90

3.30

4.30

0 Kudos
Message 5 of 12
(6,833 Views)
Hey Naqvi,
Have you seen this website?: http://forums.ni.com/ni/board/message?board.id=180&message.id=37190

This has detailed information regarding the formatting and even has a small example.


If this is all the data that you need to bring in then this should read in those values to val1,2, and 3 for you. (NOTE: if there is an empty space between each value then you should add another "\n" in between each "%f"

FILE *filename;
int val1, val2, val3;
 
filename = fopen ("filename.txt","r");
fscanf(filename,"%f\n%f\n%f", &val1, &val2, &val3);
fclose(filename);

Hope this helps!

Lars
Message 6 of 12
(6,790 Views)

Of course, as you want to read floating point values I'm sure Lars intended to define val1, val2 and val3 as type float. If you prefer to use doubles, the fscanf line would look to be:

    double val1, val2, val3;

    fscanf (filename, " %lf %lf %lf", &val1, &val2, &val3);

The space character (' ') is equally valid as a separator between fields as the newline one ('\n'), but I find it more intuitive and clearer. The exact syntax of the specification field depends on the exact contents of your file: scanf functions are generally quite complex and can be a challenge to get right. I always prefer to read a line of text from the file into a temporary string buffer first and then use sscanf() to parse it. This allows for easier error handling if things go wrong and also makes debug easier.

JR

Message 7 of 12
(6,769 Views)

Hello, Thanks very much for you reply , i have read the Data from the  text file, but now i want to know  one more thing ,

 

that is  i also want to plot the data on graph ,  i  use PLOT XY   function , and all work fine, 

 

but the problem is   this function  is not plot the negative values ,  Is there a way to plot the negative values using labwindows ???

0 Kudos
Message 8 of 12
(6,658 Views)

CVI can treat and plot negative values as well as positive ones without any particular provision, so if you're not seeing them in your diagram the reason can either be that you've not enabled autoranging on graph Y-axis (or you've not set its scale to a reasonable range: check in the UIR editor) or something in scanning routine or the following code prevents you from obtaining negative values from the file. To check the second point place a breakpoint on PlotXY line and examine the array your are to plot in the variable window: pressing Shift-F4 will bring up a small diagram popup for the array so that you can see its values at a glance.

If this does not helps, post the relevant part of your code (variable definitions included) so that we can take a look at it.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 9 of 12
(6,647 Views)

Hi , my code is this , so i just put the example data in the array and want to plot but  this code is not plotting the Negative Values. 

 

 

int CVICALLBACK Get (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 unsigned int f[8];
 f[0]=10;
 f[1]=0;
 f[2]=20;
 f[3]=0;
 f[4]=0;
 f[5]=-10;
 f[6]=0;
 f[7]=10;
 switch (event)
 {
  case EVENT_COMMIT:
   
    PlotY (panelHandle, PANEL_GRAPH, f, 8, VAL_UNSIGNED_INTEGER, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);
     SetAxisScalingMode (panelHandle, PANEL_GRAPH, VAL_LEFT_YAXIS, VAL_MANUAL, -60.0, 60.0);

   break;
  case EVENT_RIGHT_CLICK:

   break;
 }
 return 0;
}

0 Kudos
Message 10 of 12
(6,639 Views)