05-08-2012 04:14 PM
Hello all,
I am trying to fill a 2D array with values from a text file. Simple enough, right?
The problem is, I need to take values from each line and rearrange them before adding them to the array.
Here are the contents of the text file:
"SHGC0" 0.005771 0.007884 0.009891
"SHGC1" 0.662647 0.600662 0.541796
"VT0" 0.000000 0.000000 0.000000
"VT1" 0.656876 0.592777 0.531905
I need to "columns" of data and transpose them into rows, ie. [0.005771, 0.662647, 0.000000, 0.656876] (first column) and then place them end to end (would end up with 1 row, 12 columns).
The text file above would end up looking like this:
[0.005771, 0.662647, 0.000000, 0.656876, 0.007884, 0.600662, 0.000000, 0.592777, 0.009891, 0.541796, 0.000000, 0.531905]
Please note - the data will always have 6 decimal places, but the values will change depending on the text file.
- the text file is provided by a program and is not tab delimited, so making it into a spreadsheet would not help.
Thanks,
Laura
05-08-2012 04:33 PM
Yes, converting to a spreadsheet is going to be the easiest approach here. Just set the delimiter to a space (there's a constant for this in newer LabVIEW versions, but a string constant containing a space is fine too). Assuming the initial string will never contain a space, you can read an array of strings, delete the first column, then do some array manipulation (I'm thinking transpose array and reshape array here) and convert to floating point. If the initial string might contain a space, then you can do the same thing but line by line, removing the string and then reading an array of floating point values.
05-09-2012 07:05 AM
Hi
Could you post your text file? We can try out the options available and then make a better suggestion.
If you have already developed a vi to read from the text file, post that too. We can see what the problem could be.
05-10-2012 08:47 AM
That's a fairly straightforward operation, since LabVIEW has a goodly selection of string and array manipulation tools. The attached code is in LV 2010.
The VI reads the entire file (you may want to chunk this if you have a huge file). Using the spreadsheet string to array primitive, this is converted to an array of strings using the space as a separator. The first column is deleted, the array transposed, then reshaped to a linear array. If you have further questions, let us know.