From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

2D array becomes (sub)array when attempting to write

Solved!
Go to solution

I'm trying to write a 2D array to a file (want a text file), but it seems to change it to a 2D (sub)array when I connect it to the Write to Text File, making the terminals different types, causing an error. However, the front panel is able to display the array, as long as it's not connected to the Write to Text File.

 

Can I get some help with this? [Maybe I'm using a bad method to put the two arrays together…] I'm using LabVIEW 12.0.1, but saved it for 8.0.

0 Kudos
Message 1 of 6
(2,725 Views)

To use "write to text file", first use "array to spreadsheet string".

Message 2 of 6
(2,719 Views)

@Quevvy wrote:

I'm trying to write a 2D array to a file (want a text file), but it seems to change it to a 2D (sub)array when I connect it to the Write to Text File, making the terminals different types, causing an error. However, the front panel is able to display the array, as long as it's not connected to the Write to Text File.

 

Can I get some help with this? [Maybe I'm using a bad method to put the two arrays together…] I'm using LabVIEW 12.0.1, but saved it for 8.0.


A "text file" has no concept and understanding of arrays of any dimensions. It is just a long string of characters and that's the only thing "write to text file" accepts! To define a 2D array in a text file, you need to define column delimiters (typically "tab") and row delimiters (typically "newline"). That's what "write to spreadsheet file" does for you. "read from spreadsheet file"  reverses the operations and translates the spreadsheet string stored in the file back to a 2D array. (alternatively, you can explicitely translate between string array and spreadsheet string and vice versa using the primitives mentioned above. This is not any different than using "read/write from/to spreadsheet file", so you are not really gaining anything).

 

One problem with the described method is the fact that the delimiter characters are of course not allowed as part of the text array elements, potentially limiting the general usefulness.

 

Can you explain what you expect to be in the file and how it is supposed to be read later.

 

Another possibility would be to write it to a binary file. It really depends on your exact needs, but you haven't really told us much.....

 

 

 

 

Message 3 of 6
(2,691 Views)

The final format of my data file will be

 

Variable 1: 4236.5234

Variable 2: 234.34

Variable 3: 213.8218

...

 

Time (s)  Data 1    Data 2    Data 3

0         10.1      0.02      7.8

1         10.2      0.03      7.6 

2         ...       ...       ...

...

 

The only thing is, Variables 1, 2, 3 aren't calculated until the user presses "Store Data" or when the VI stops (the variables are averages, maxes and mins); whereas Time & Data 1, 2, 3 are calculated at each time interval. The way I currently have it set up, I first write the labels for Variables 1, 2, 3 and "test" as their values (all are strings) using 2D array -> spreadsheet string, then Write to Text File. Then I write the labels for Time, Data 1, 2, 3, using the same method. At each time interval, the data is acquired, built into an array, and then written to the data file via Write to Spreadsheet File using append?≡TRUE. In order to get the values for Variables 1, 2, 3, I then go back and overwrite the beginning of the file with another 2D array -> spreadsheet string. However, this ends up overwriting the "Time (s)" label a bit.

 

The end goal of this data file is to be imported into Microsoft Excel 2007.

 

I did it this way because I could not find a way to insert the "Variables 1, 2, 3" 2D array at the beginning of the data file. Any data file formatting help would be appreciated.

0 Kudos
Message 4 of 6
(2,638 Views)
Solution
Accepted by topic author Quevvy

One way is to write the "Time (s)  Data 1, ..." to a temporary file, then when you know the Variable 123 data, write that to a real file, then read the temp file as a 2d array of strings, and append it to the real file.

Message 5 of 6
(2,619 Views)

@Todd_Lesher wrote:

One way is to write the "Time (s)  Data 1, ..." to a temporary file, then when you know the Variable 123 data, write that to a real file, then read the temp file as a 2d array of strings, and append it to the real file.


Yeah, I had thought about that before, but I didn't want to mess with creating/deleting a file. But in the end, it is probably the best solution. I had a few issues with deleting the temporary file, but I got it all dealt with. Thanks!

0 Kudos
Message 6 of 6
(2,596 Views)