LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to read scaled data in Matlab?

I'm using the board PCI-6115, Labview and Matlab.
I have some questions related to that scaling of data. I'm using as base of my program the example:'Cont Acq to File(binary)', present in Ni Help. I acquire the data as i16(unscaled) and then read it in Matlab using the low level I/O functions:
fin=fopen('data.bin','r','b')
data=fread(fin,[5000,4],'int16')
Everithing is ok, but the data is unscaled.How can I scale the data?
I tried to modify the program to write to the file scaled data, but when I read it in Matlab, using the same procedure, I only get a matrix with 0. Does anyone know what is the problem?
Thanks in advance
0 Kudos
Message 1 of 11
(7,510 Views)
Hi pmad1,

Have you tried saving scaled data (SGL precision) in LabVIEW, and then read from matlab with 'single' instead of 'int16' as fread's parameter?

Just a thought. I don't have MatLAB here to test it out.

Dan
0 Kudos
Message 2 of 11
(7,502 Views)
Hello Sir,
I am facing the similar problem. Actually I tried everything on Matlab. Finally I am getting expected float values in the output. I am getting the 1st, 3rd, 5th and so on - correct values and 2nd, 4th, 6th and so on - all zeros. I dunno the reason. When I have edited the same binary file (which is the output of attached VI) in hex editor I able to see some data like

00000000 41 A2 EC 0F 00 00 00 00 41 A2 EC 0F 00 00 00 00
00000010 41 A2 88 1B 00 00 00 00 41 A2 88 1B 00 00 00 00
00000020 41 A2 88 1B 00 00 00 00 41 A2 88 1B 00 00 00 00
00000030 41 A2 88 1B 00 00 00 00 41 A2 88 1B 00 00 00 00

I am not able to understand the data contained in it and the reason why I am getting the zeros in between. When Labview stores the file in binay format without header (am using SGL precision as you have mentioned), does it store the data with some zeros in it? I would be thankful to you if you can help me in resolving the problem.

Have a Good Day

Regards,
Krishna
0 Kudos
Message 3 of 11
(7,440 Views)
Hi Everybody!

I have attached a VI (Write MAT file.vi - written in LabVIEW 7.1) that takes a waveform and directly converts it to a 2D array where the first column is the timestamps and the second column is the data points. You can then pass this 2D array of scalars directly to the Save Mat.vi. You can then read the .MAT file that is created directly from Matlab.

For more information on this, you can reference the following document:
Can I Import Data from MATLAB to LabVIEW or Vice Versa?
http://digital.ni.com/public.nsf/websearch/2F8ED0F588E06BE1862565A90066E9BA?OpenDocument

However, I would definitely recommend using the Matlab Script node (All Functions->Analyze->Mathematics->Formula->Matlab Script). In order to use the Matlab Script node, you must have Matlab installed on the same computer. Using the MatlabScript node, you can take data generated or acquired in LabVIEW and save it directly to a .mat (Matlab binary) file using the 'save' command (just like in Matlab). You can see this in example VI entitled MathScriptNode.vi - written in LabVIEW 7.1.

I hope this helps!
Travis H.
LabVIEW R&D
National Instruments
Message 4 of 11
(7,423 Views)
pmad1, I worked on this last year. Here is the .m file I created to do what you want. I'm pretty sure I was using the exact same example from LV to write the bin data, so this should be exactly what you need. If not, it should lead you to your solution.

Chris

doh! I have to change the extension to .txt to upload it. You can change it back to .m.
0 Kudos
Message 5 of 11
(7,417 Views)

Hi

 

I'm not sure you still remember this problem.  I'm very surprised when I found this thread. 

I have an exactly same problem.

Now I'm using the PCI-6115 and have an I16 unscaled data from the 'Cont_Acq_to File(binary).vi'. 

To analyze the saved data, I'm going to use the matlab, because in labview it takes lots of time. 

However I can not read those data in Matlab.

 

So, could you tell me how can you solve this problem?

I tried the matlab code, end of this thread by C. Minnella, but it does not work for me.

Do I need to something for this code or am I miss something?

 

Thanks in advance.

Have a good day.

0 Kudos
Message 6 of 11
(6,784 Views)
So, I just took a look at that m-file that I posted. It looks like that was designed to read data acquired from the example stream to binary that came with Traditional DAQ. The principles are the same. I am assuming the example vi that you are referring to is "Cont Acq&Graph Voltage-To File(Binary).vi" that is under DAQmx. There is another example, "Graph Acquired Binary Data.vi", that shows you exactly the steps you need to take to read the binary files. You can re-create this vi in Matlab. Some of the tricks are revealed in the m-file referenced above (for example, you need to read the data big-endian).

To start, you read two 8byte doubles from the file (the first is the # of coefficients, the second is the # of channels). Then you read the scaling data (an array of doubles with a length equal to #coefs*#channels) which you can do what you want with. Finally, you read the remainder of the file (equal to the total file length minus the current file mark). In the example, this is an array of I32's, but you'll want to change that to I16's (assuming you did the same when you wrote the file). In the example vi they got a bit fancy and took advantage of a feature of the read from binary function to format the data as a 2D array on the output. In Matlab, you can reshape the vector of I16's that's returned from fread the same way. It has rows = to the number of scans, and columns = to the number of channels. You can see how you can determine that from the example. The ~tricky part is recreating the scaling vi that takes that scaling data and the 2D array of I16's and returns an array of waveforms. It's not that complicated; you should be able to figure it out by reviewing the example. Try this, and post back if you have problems.

<rant/>One side note: there is a bug in "Cont Acq&Graph Voltage-To File(Binary).vi" in the "create header.vi" part. I've pointed this out before, but have not gotten any traction on getting it fixed (it's been there at least since 7.1, and I'm looking at it in 8.20 now). This won't effect you if you don't set different ranges for each channel during your acquisition (every row of your coefficients array will be the same). The bug is that the array is built backwards; for every channel the new scaling parameters are stored at the beginning instead of the end. The result is that if you have four channels with four different sets of scaling parameters:
Channel 0 coefficients, Channel 1 coefficients, Channel 2 coefficients, Channel 3 coefficients, the output will go in reverse order: 3, 2, 1, 0. This is NOT correct, and it isn't a feature.</rant>

Chris
Message 7 of 11
(6,778 Views)

Hello

 

Wow, I didn't expect an answer for me so quick and from you.

Actually, I did follow every your writing. I'm a kind of new labview user (only 2 month),

so I don't know much about the labview, but I think you're great.

I also read the writing about the bug in Cont Acq&Graph Voltage-To File(Binary).vi. in other thread. 

I don't know much about that, but I think you're right for that.

 

You're exactly right for my labview.  I modified two vis from example, "Cont Acq&Graph Voltage-To

File(Binary).vi" and "Grah Acquired Binary Data.vi".  Actually I only changed one part, save I16 and read I16.

I need a 5MHz sampling data and it's so big.  That's why try to use Matlab.

 

I copied, pasted your matlab code and made LVReadBIN.m file.

When I run it with my data file, I got this error.

 

===============

>> LVReadBIN(1000);

??? Undefined function or variable 'then'.

 

Error in ==> C:\Th\LabView\NI_6115\10192006\LVReadBIN.m (GetHeader)

On line 103  ==>     then

 

Error in ==> C:\Th\LabView\NI_6115\10192006\LVReadBIN.m

On line 23  ==> headerInfo = GetHeader(fileName,pathName,dataBytes);

>>

================

I'm not an expert for the Matlab, but I don't think the "then" is not right commend,

so I deleted and run again.  And I got another error.

===============

>> clear

>> LVReadBIN(1000);

??? Error using ==> fread

Out of memory. Type HELP MEMORY for your options.

 

Error in ==> C:\Th\LabView\NI_6115\10192006\LVReadBIN.m (GetHeader)

On line 104  ==>     userHeader=fread(fid,userHeaderLength,'uchar');

 

Error in ==> C:\Th\LabView\NI_6115\10192006\LVReadBIN.m

On line 23  ==> headerInfo = GetHeader(fileName,pathName,dataBytes);

>>

============

I attached 5MHz sampling data file. I have to change the name. It's not a txt file.

You just change the name (extension) from "test-5M.txt" to "test-5M.bin".

 

If you don't mind, could tell me what my problem is? Am I miss something or did wrong?

 

I really appreciate for that.

Thank you very much.

 

Have a great day.

Best regard.

Aggie.

0 Kudos
Message 8 of 11
(6,773 Views)
Aggie, this should solve your problems (and others who ask the same question). Here is an m-file function and an example that shows how to read binary data generated from "Cont Acq&Graph Voltage-To File(Binary).vi" into MATLAB. It works on the sample data you provided (you acquired 0.1 seconds of data at 5MHz on a single channel, right?).

Don't forget to change the extensions from .txt to .m.

Don't be afraid to throw me some stars! Good luck.
Chris

Download All
Message 9 of 11
(6,759 Views)

Thank you very much Chris.

 

It's really great. If I can, I want to give you 100000000 stars.

 

Now I saw your code and checked my data with your program. It works perfectly.

My original data file is much bigger than the sample data, so from now, I need to think about how can I split the data and analyze it to avoid memory problem.

 

I want to say again that I really appreciate for spending time for this.

 

Have a good day.

Best regards,

Aggie.

 

P.S. I know this forum is for Labview, but I have a question about Matlab.

As I said before, my original data is very big (5MHz sample with over 10 seconds).

Whenever I run it, I have a memory full message. If you don't mind, could you give me any advice or tip for this matter?  If you don't want to write about matlab in NI forum here, you can send e-mail to me.

bigbabo@hotmail.com.

Anyway, if you don't have time for this, that's fine. You already did lots of things for me !!!!

Thank you

0 Kudos
Message 10 of 11
(6,717 Views)