LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Read datas of .txt file according to the list using CVI

I have stored great a deal of datas according to the date and time in a  .txt  file using CVI.

Now I want to read them according to the list , so the datas can be displayed in the strip chart according to the  time sequence. The historical datas' playback could be realized.

However,I don't know how to read datas according to the list. Let alone with mention indexing according to the time sequence.

The following is a part of  the data of my storage.


04-12-2011  14:30:58     WJ 220, MV 27.347,MA 0.114
04-12-2011  14:31:08     WJ 219, MV 27.366,MA 0.075
04-12-2011  14:31:18     WJ 221, MV 27.354,MA 0.087
04-12-2011  14:31:28     WJ 221, MV 27.348,MA 0.112
04-12-2011  14:31:38     WJ 221, MV 27.347,MA 0.11


I need to put the WJ , MV and MA datas  in chronological order in the strip chart shows.

Anyone who have good idea ?

 

Thanks.

Best regards.

 

xiepei

I wouldn't care success or failure,for I will only struggle ahead as long as I have been destined to the distance.
0 Kudos
Message 1 of 20
(10,920 Views)

Hi xiepei,

 

First, you should stick to your original post here rather than duplicating it.

 

Second, in C you can use scaning and formatting functions to achieve things like that - did you have a look at the corresponding part of the help? You could use either the ANSI C or the Formatting and I/O library of CVI.

0 Kudos
Message 2 of 20
(10,916 Views)

Hi xiepei,

paste the following lines in the interactive execution window and check that data scanning is performed correctly.

 

Basically, you need to open the file in ASCII mode (OpenFile) and read line-by-line (ReadLine), scanning each line as in the example and accumulating results in double arrays.

 

Data can then be plotted on a XY graph (better than a stripchart) provided that the X axis is formatted in absolute time: it will automatically display time figures Smiley Happy

 

 

#include <formatio.h>
#include <ansi_c.h>

static double		wj, mv, ma, et;
static char		line[512];
static struct tm	ti;

// One sample line of yours
strcpy (line, "04-12-2011 14:30:58 WJ 220, MV 27.347,MA 0.114");
// Scan the line separating extracting fields
Scan (line, "%d[x]%d[x]%d[x]%d[x]%d[x]%d[x]%s[dt#]%f[x]%s[dt#]%f[x]%s[dt#]%f", &ti.tm_mday, &ti.tm_mon, &ti.tm_year, &ti.tm_hour, &ti.tm_min, &ti.tm_sec, &wj, &mv, &ma);

// Adjust fields for tm struct correct format
ti.tm_mon--; ti.tm_year -= 1900;
// Calculate timestamp
et = mktime (&ti);

 

My example makes intensive use of functions from the Formatting and Scanning Library: I suggest you to carefully study Scan () function help and examples, as it is a powerful instrument that has saved me several headaches while parsing data files Smiley Wink

 

 

Finally, I second wolfgang warning: you should better post additional informations to your original post to avoid duplicating threads.

 



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 3 of 20
(10,923 Views)

Hi !

Thank you for your help.

I didn't know what to do when I wrote my original post. 

Maybe I could read the datas from the .txt file and then put them in the strip chart later on.

But I don't know how to read the datas according the list. So I wrote this post.

You advised me to use scaning and formatting functions and I have had a look at the corresponding part of the help.

However,I am still not sure which function to use in  the ANSI C or the Formatting and I/O library of CVI .

My C programming basis is little poor.Could you give me a clear point ?

If I can get your help ,it is really too good. Thank you very much indeed.

 

Best regards.

xiepei

I wouldn't care success or failure,for I will only struggle ahead as long as I have been destined to the distance.
0 Kudos
Message 4 of 20
(10,906 Views)

Thank you for your reply.

I use the statement "fileHandle=OpenFile ("e:\\CVI\\psm.txt", VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);"

and the statement " ReadLine(fileHandle,oneLineData,100);" to read the datas line-by-line.

The above mentioned I think is right.

There are many lines in my file.It is unpractical to scan each line as in the example and accumulate results in double arrays in my procedure.

In this scenario, more columns contain a large amount of data. I want to put each column data displayed on the strip chart in chronological oder.

Besides,  I just want to take part of them out displayed on the strip chart.

Such as "外部交流电压, 主回路电压,主回路电流" in the attachment.

What about the function FileToArray ?

If I  save my data as a csv file format and load it in excel, is it easier for multicolumn data derived ?

Finally,thanks for your information.I will try and remember to aviod  duplicating threads.

 

Best regards.

xiepei

 

I wouldn't care success or failure,for I will only struggle ahead as long as I have been destined to the distance.
0 Kudos
Message 5 of 20
(10,900 Views)

Hi !

Because it is difficult  to put each column data out and display them on the strip chart in chronological oder. 

Is it available to observe previous waveform by moving x axis on a strip chart ?

So we can realize the history datas curve playback.

 

Thanks !

 

Regards.

 

xiepei

I wouldn't care success or failure,for I will only struggle ahead as long as I have been destined to the distance.
0 Kudos
Message 6 of 20
(10,898 Views)

xiepei,

some answers to your last posts.

 

1. Functions you have noted should work, simply use ReadLine inside a loop to continuously read the file line by line; trap function return code and exit the loop if -2 (end-of-file reached)

 

2. Depending on how large is the data, reading all content can be a practical solution together with zooming and panning capabilities of the graph control: you may load all data on the graph letting the user to interactively expand the region of interest

 

3. FileToArray is not a feasible solution in your case as you have mixed text/number data in your file. FileToArray works only when you have a constant data type throughout the file

 

4. "I just want to take part of them out displayed on the strip chart" I don't understand if you want to select a region in the file (e.g. base on a date/time interval) or if you want to select only part of each line (e.g WJ column only). In the first case you can scan the file until you find the starting date, next scan lines extracting data values and exit the loop at the first date past final selection date. In the second case, adding a 'd' inside the scan format string (e.g. "%d[dx]") tells the program to scan that part of the line and discard the corresponding data: you do not need to provide an array for that column

 

5. Passing through Excel can be an easy task if done interactively. Definitely is not so simple if done programmatically. What do you expect to do with Excel? Simplify the scanning part, simplify the graphing part? In both cases in my opinion this introduces a not-so-easy task in your application; since you said you are quite new to CVI I suggest you to run go through scan-and-display option first



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 7 of 20
(10,889 Views)

Hi !

 I'm sorry to reply you for so late.

You are right.It's better to plot data on a graph than a strip chart.I've tried it when I have a constant data type through the file using FileToArray.

if(FileSelectPopup ("", "*.txt", "*.txt", "Openfile", VAL_OK_BUTTON, 0, 0, 1, 0, path))
      {
        GetFileInfo(path,&filesize);
       n=sizeof(double);
       frdata=(double*)malloc(filesize);
    FileToArray ("外部交流电压.txt", frdata, VAL_DOUBLE, filesize/(2*n), 1, VAL_GROUPS_TOGETHER, VAL_GROUPS_AS_ROWS, VAL_ASCII); 

   PlotY (psmppanelHandle, PSMP_PANEL_GRAPH_WJ, frdata, filesize/(2*n), VAL_DOUBLE, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);

    }

Although it can read the data from the .txt file and plot the data on a graph, the effect is not very ideal.

There are mainly three unsatisfactory place as the following.

1.I have to save the data in several more files.There is always a file appearing garbled. It's the file "外部交流电压.txt."

see the attachment:外部交流电压.txt

  The function code to store datas is:

        f = fopen ("外部交流电压.txt", "a+");
            if( f != NULL )
            {
              fprintf(f," %d",wy_voltage);
              fclose(f);
             }

2.It must open the dialog box to choose the data playback file every time.

3.It displays the entire file data and it could not choose the data to plot on a graph.  

So I want to select only a part of each line(e.g WJ column,WV column and WA column) in the file based on a data/time interval. In this case,it's a bit complicated.I should scan the file until I find the starting data ,next scan lines extracting data values,adding 'd','g','g' inside the scan format string(e.g." %d, %g, %g") and exit the loop at the first data past until final selection data.What do you think for this idea ?

Which function do you think is best to scan the file to find the starting data .I have no idea which function to use among "Scan" ,"ScanFile"and"ScanIn".

If I save my data as a csv file format and load it in excel , I will  expect to realize the same function as the .txt file with excel.

Thank you very much !

Best regards.

xiepei

I wouldn't care success or failure,for I will only struggle ahead as long as I have been destined to the distance.
0 Kudos
Message 8 of 20
(10,868 Views)

Xiepei,

I am now ot of office and have no much time to respond, but I will give you some ideas for you to elaborate on.

 

Let's start examining your code:


if(FileSelectPopup ("", "*.txt", "*.txt", "Openfile", VAL_OK_BUTTON, 0, 0, 1, 0, path))

      {
        GetFileInfo(path,&filesize);
       n=sizeof(double);
       frdata=(double*)malloc(filesize);
    FileToArray ("外部交流电压.txt", frdata, VAL_DOUBLE, filesize/(2*n), 1, VAL_GROUPS_TOGETHER, VAL_GROUPS_AS_ROWS, VAL_ASCII); 

   PlotY (psmppanelHandle, PSMP_PANEL_GRAPH_WJ, frdata, filesize/(2*n), VAL_DOUBLE, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);

    }


First of all, you are getting the full file pathname from FileSelectPopup but you are not using it in OpenFile!

Second, your way to calculate the array size is far from optimum: since you are reading an ASCII file you have no warranty about the lenght of your data: sizeof (double) returns 8, but depending on the precision a string represtnatation of a double number can take more or less than this (e.g "3.14" vs. "12345.2094937101841" Smiley Wink ). Your way of calculating array size could work if used on binary files (even if it would need some trim).

 

 

Now let's go to your questions.

 

Q1. File garbled. You may need to add a "\n" to fprintf to separate data in rows.

Q2. See above. Additionally, with some little trick you can automate the file opening, for example if your files are named "filename.wj", "filename.wv", "filename.wa"

Q3. You can always plot a subset of values: supposing you have an array of 10000 elements, to plot only 1000 elements starting from item 2000 you can do this:

PlotY (..., ..., array + 2000, 1000, ..., ..., ..., ..., ..., ...);

 



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 20
(10,864 Views)

Hi !

Thank you so much for giving me guidance an short notice.

I've put the code modified as the following.

 if(FileSelectPopup ("", "*.txt", "*.txt", "Openfile", VAL_OK_BUTTON, 0, 0, 1, 0, path))
      {
        GetFileInfo(path,&filesize);
     n=sizeof(double);
     frdata=(double*)malloc(filesize);     
    FileToArray (path, frdata, VAL_DOUBLE, filesize/(2*n), 1, VAL_GROUPS_TOGETHER, VAL_GROUPS_AS_ROWS, VAL_ASCII);
    PlotY (psmppanelHandle, PSMP_PANEL_GRAPH_WJ, frdata, filesize/(2*n), VAL_DOUBLE, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);
    
       }

It could also work. It's no longer appearing garbled in the file when I add a "\n" to fprintf to separate data in rows.

Finally,I want to trouble you another thing.Could you tell me an optimum way to calculate the arrary size , since I am reading an ASCII file? You can respond me when you have time.  Thanks again for your help.

Best regards.

xiepei

 

I wouldn't care success or failure,for I will only struggle ahead as long as I have been destined to the distance.
0 Kudos
Message 10 of 20
(10,847 Views)