01-28-2012 08:38 AM
Dear forum users,
I want to determine the size of an array subset in bytes.
At the moment I try to do this as shown in my attached program: Simply said, I am reading in my data (example data can also be found in the attachment) and convert them into an array. From this array I am taking the subset I am interested in. This array subset is saved in a new file from which I determine the byte size with the “Get File Size.vi”.
That are a lot of steps with file saving (deletion of the compulsory created new file not mentioned) just to get the byte size of this array subset. And with this approach the file size (the byte size of my array subset) is unfortunately depending on the format with which I save the file (%.6f or similar), but I would need the byte size of this subarray as it would be in the original file (see my comment on the front panel).
Therefore, I want to know if there is a better, simpler, quicker and correct way to get the byte size of a subarray.
I would be very happy about every comment and help!
01-28-2012 09:03 AM
01-28-2012 09:11 AM - edited 01-28-2012 09:12 AM
Your question is ambiguous/unclear because you refer to the number of bytes for the array, but you're talking about strings. When you write to file (or use a string) you are not determining the number of bytes of the array. You're determining the number of characters (letters) in the file/string. A 32-bit integer is a 32-bit-integer is a 32-bit integer. It always takes up 4 bytes. However, when you write it to file it can take up more than that. For example, the value 123456789 is a 32-bit integer. It takes up 4 bytes in a computer. When you write it to a text file it takes up 9 bytes.
I can't look at your VI right now, but the number of bytes for an array will be the number of elements times the storage size for the datatype + array header. This is documented in the LabVIEW Help file.
01-28-2012 09:22 AM
Hi saverio,
tho OP is creating a "tab separated value"-file with a format code of "%.6f", thus the "file length" or the number of bytes written may change depending on the data to write. See my snippet for a simple solution - whereas the OP was using WriteToSpreadsheetFile and GetFileSize...
01-28-2012 10:48 AM
Dear GerdW and smercurio_fc,
Thanks a lot for your replies! Perhaps I should have mentioned that I need the size of my array (string…, I am confused now) in bytes because I want to use later in my program the “Set File Position.vi” (which needs an offset in bytes). With this vi I want to read in my data again but starting after the data which I have used in my array (I try to make bunches out of my file).
Ok, the suggestion of GergW is a step forward, but it does not work correctly. I have modified my program a bit (see attachment), so that it gives back the file size of the original file and the length of the string after the file has been read in. Interestingly, the numbers are not the same. The same is also true for my subarray (and the string length is still dependent on the format with which I convert my array to a string).
So once again asked: If I take from my original file lines 3 to 12, copy them in a new file and save this file, then I can read out the size of the file (191 bytes). How can I get the same number in LabVIEW after reading in the original file and taking the same subarray? I hope my question is clearer now (it is surely suffering a bit from my bad English. I am sorry!).
I hope to hear from you again!
01-28-2012 11:13 AM
Convert EOL. Read the help file. The Detailed help for Read from Text File says:
"The function converts all platform-dependent end-of-line characters to line feed characters unless you right-click the function and remove the checkmark next to the Convert EOL shortcut menu item."
Removing the check mark made the numbers match.
What is gained by having the sequence structure in your VI? Dataflow will cause the file to be closed after reading even if the sequence structure is removed.
Lynn
01-28-2012 12:00 PM
Dear Lynn,
Thanks for enlighten me a bit! After removing of the check mark the numbers are fitting!
But that does not solve the actual problem….
How can I get back the size of my (sub) array ((sub) string after the conversion) in bytes?
Ah, by the way, the sequence was there because it was in used in my first program, and I forgot to delete it. Thanks for the hint!
01-28-2012 01:45 PM
@GerdW wrote:
Hi saverio,
tho OP is creating a "tab separated value"-file with a format code of "%.6f", thus the "file length" or the number of bytes written may change depending on the data to write. See my snippet for a simple solution - whereas the OP was using WriteToSpreadsheetFile and GetFileSize...
Yes, I know what your snippet was doing. It wasn't clear to me whether the OP was actually asking for the number of bytes of the array, or the number of characters of the file, which are two different things. And, if the file wasn't being written out by LabVIEW, but it came from someplace else, you could actually have 2 bytes per character (Unicode).
01-28-2012 02:46 PM
There seem to be two questions here -- one is to determine the size of an array, in bytes, and the other has to do with how data are written to a file (to be able to use the file positioning functions properly).
The snippet below only deals with the first question, determining the size of the array, in bytes. I did it 4 times, twice each for a 2D array (I used an array of doubles, but the technique works for any array of "constant-sized things"). The first method is to determine the number of array elements (using Array Size), and the number of bytes in a single element (using Flatten to String and String Length). The second method simply flattens the entire array to a string and determines its length -- while this is conceptually simpler, it is "computationally inefficient" (unless you have a need for the string), especially for really big arrays (you'll be creating a "duplicate array" in string format that you might not need except to determine its length).
01-28-2012 03:25 PM
Thank you Bob for your reply!
I think you understood perfectly what I want to have (determination the size of an array in bytes)! The second question came up because of my first approach (conversion of array to string).
I like your suggestion, but as you write, this can only work for "constant-sized things", but that is unfortunately not the case for my data. If I apply your first suggestion, I get for my array 240 and not 191 as solution. Well, perhaps there is a possibility to adapt your program to my needs. I do not know…
My new idea is at the moment the following (see vi in the attachment): I search in my string (which I get after data read in) for the first and last element of my sub array, and then I calculate the length of a newly created sub string. The result is 193! That is almost the byte size (191) but I do not know why there is a difference of 2. And I am not sure if this is a nice solution. I fear it could cause problems for large files.
Anyway, I would be happy about feedback or new suggestions!