LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Read file and load into two separate arrays

Hello to all,
 
 
I want to read a file of 100 elements. Out of these 100 elements, I want to load the first 50 into an array called "high" and the remainding 50 into an array called "low"
 
I am new to CVI, how can I do this?
 
So far I can load the first 50 into high but am having the issue with the remainder. I am using the following code.
 
FileToArray ("C:\\data.ini", high, VAL_DOUBLE, 50, 1, VAL_GROUPS_TOGETHER, VAL_GROUPS_AS_COLUMNS, fileType);
 
Thanks!
0 Kudos
Message 1 of 4
(3,403 Views)

The easiest thing is to use Copy1D functions into the Analisys library. Y could use the following:

1. Dimension a temporary array of 100 elements (calloc...)
2. Read all the content of your file into tat temporary array:

FileToArray ("C:\\data.ini", temp, VAL_DOUBLE, 100, 1, VAL_GROUPS_TOGETHER, VAL_GROUPS_AS_COLUMNS, fileType);

3. Copy the firts half to High array:

Copy1D (temp, 50, High);

4. Copy the second half to Low array:

Copy1D (temp + 50, 50, Low);

5. Free your temporary array



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 2 of 4
(3,400 Views)
Thanks-Alot-Roberto,
 
What if...
 
On my text file, I want to place headers.
Currently my file is...
 
1.0
2.0
3.0
4.0
 
what if my file were to be...
high[0] = 1.0
high[1] = 2.0
low[0] = 1.0
low[1] = 2.0
 
 
I modified the code and received this error
FileToArray ("C:\\data.ini", array, VAL_CHAR, 10, 1, VAL_DATA_MULTIPLEXED, VAL_GROUPS_AS_COLUMNS, fileType);
Copy1D (array, 5, high);
Copy1D (array + 5, 5, low);
 
134, 27   Type error in argument 1 to `Copy1D'; found 'pointer to char' expected 'pointer to const double'.
"Does this mean I can't use the Copy1D function?
 
0 Kudos
Message 3 of 4
(3,396 Views)


@FredTest wrote:
 
Does this mean I can't use the Copy1D function?


Exactly! Copy1D, like almost all the functions in the Analisys library, treats ONLY double precision variables and arrays.
To read a file with the structure that you are tracing you'll have to read it line-by-line and scan the rows to separate header from data: I suggest you to use OpenFile, ReadLine, Scan or sprintf and CloseFile functions to accomplish this task.
 
Another options could be to give a slightly modified structure to your file and treat it as an INI file with the appropriate instrument driver. Supposing your file can be arranged in this or similar way:
 
[DataHigh]
high0 = 1.0
high1 = 2.0
 
[Datalow]
low0 = 1.0
low1 = 2.0
you could use IniFile instrument driver ( \toolslib\toolbox\inifile.fp ) to read the file element by element.


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 4
(3,385 Views)