LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Problems writing a structured binary file...

Hi Folks,

 

I've been working with one of my students to convert this matlab routine into a VI to no avail:

 

fwrite(fout,version,'char');

fwrite(fout,nchar_text,'short');
% convert wave_text into array of ascii integers
fwrite(fout,wave_text,'char');
% output a null character to terminate string
%count=fwrite(fout,0,'char');
fwrite(fout,n_bits,'char');
fwrite(fout,n_bytes,'char');
fwrite(fout,data_polarity,'char');
fwrite(fout,user_data,'float');
fwrite(fout,s_rate,'ulong');
fwrite(fout,adrange,'float');
fwrite(fout,n_pts,'long');
fwrite(fout,wave,'short');
fclose(fout);

 

I've written a VI that can open the files written by this Matlab procedure with no problem (see openfile.VI), however I can't seem to recreate the method for writing this file to binary (writefile.VI).  I think the Matlab approach is very similar to C, and I'm guessing the issue has to do with converting datatypes effectively.  I've been searching the forms and google, but can't seem to come up with the proper solution.  Any pointers would be greatly appreciated!

 

Best,
Jason Gallant

 

 

Download All
0 Kudos
Message 1 of 9
(3,465 Views)

When you read the file, wire a constant of the same datatype into the top as you used to write the data to the file.

0 Kudos
Message 2 of 9
(3,453 Views)

Reading the file isn't the problem, it's writing the file!  I'm able to write them with Matlab, but I can't write a routine in Labview that can write them.  This is what we are struggling with...


@RavensFan wrote:

When you read the file, wire a constant of the same datatype into the top as you used to write the data to the file.


 

0 Kudos
Message 3 of 9
(3,438 Views)

I haven't looked at your vis in great detail but when you write the file you have prepend array or string size? set to true. Are you sure this is what you what to do? I do not know whether fwrite has the same effect. (Set it to false so this does not occur.)

 

Cheers,

mcduff

 

Message 4 of 9
(3,430 Views)

The additional bytes for the array and string lengths is what is throwing you off.  Even if you turn it off at the Write Binary File, because you built a cluster they will be in there.  You do not want to cluster up your data in this case.  You need to string together a bunch of Write Binary Files, one for each of your parts of data.  Similar to what you had to do in Matlab.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 5 of 9
(3,405 Views)

@jasongallant wrote:

Reading the file isn't the problem, it's writing the file!  I'm able to write them with Matlab, but I can't write a routine in Labview that can write them.  This is what we are struggling with...


@RavensFan wrote:

When you read the file, wire a constant of the same datatype into the top as you used to write the data to the file.


 


If you write the file using the cluster, then proceed to read the file using Binary File Read, then it will work.  And you won't have to go through all of these gyrations of saving each and every piece of the cluster individually using multiple reads and multiple writes.

Or are you trying to write a binary file that is still capable of being read by Matlab?

Message 6 of 9
(3,389 Views)

@crossrulz wrote:

The additional bytes for the array and string lengths is what is throwing you off.  Even if you turn it off at the Write Binary File, because you built a cluster they will be in there.  You do not want to cluster up your data in this case.  You need to string together a bunch of Write Binary Files, one for each of your parts of data.  Similar to what you had to do in Matlab.


Great suggestion!  We tried implementing this, and things are looking better.   I've attatched the new VI that implements this.   There still seems to be a problem, however with the datatypes being written.  As mentioned in the previous post, the data written is:

 

version   - 'char

nchar_text, 'short'
wave_text, 'char'
n_bits,'char'
n_bytes,'char'
data_polarity,'char'
user_data,'float'
s_rate,'ulong'
adrange,'float'
n_pts,'long'
wave,'short'

 

Should i be typecasting (or something similar) to string in order to make this readable by my previous routine?

 

 

0 Kudos
Message 7 of 9
(3,383 Views)

Yes, I'm trying to create a file that is readable by C programs as well as Matlab.  I have a library of previously recorded (several thousand) datafiles that I'm trying to maintain compatability with.

 

 

0 Kudos
Message 8 of 9
(3,382 Views)

Watch your representations and order that you are writing.  I see a few items that should be a char, but is an I32 (which is the default data type for an integer).


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 9 of 9
(3,373 Views)