06-09-2014 10:29 PM
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
06-09-2014 10:46 PM
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.
06-09-2014 11:33 PM
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.
06-09-2014 11:45 PM
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
06-10-2014 06:34 AM
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.
06-10-2014 09:15 AM
@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?
06-10-2014 09:46 AM
@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?
06-10-2014 09:47 AM
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.
06-10-2014 11:09 AM
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).