LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Txt File to ASCII to Binary (MATLAB equivalent??)

Solved!
Go to solution

Hi,

 

I just started using LabView two days ago and need some help with what is probably very basic stuff. My objective is to take a .txt file containing some message (letters, numbers, etc) and convert this into a serial stream of bits. In MATLAB, I would have done this like so:

 

temp=textread('CommsInput.txt', '%1s', 'whitespace', '');    %load .txt file
text = char(temp);                                                               %convert cell into character type
y = zeros(length(text)*8,1);                                                %initialize o/p vector
for n = 1:1:length(text)  
a=abs(text(n));                                                                   % find the corresponding ASCII number to each character
i = 8*(n-1)+1;                                                                     %define indexing for output vector
y(i:i+7,1)=(de2bi(a,8))';                                                      % convert this number into an 8-bit binary number (ANSII)
end

 

Ultimately, this would give me an (8*N x 1) vector where N is the number characters in the text file.

 

From here I would be able to easily access each bit to create a Pulse Position Modulation signal I want to use for laser communication.

 

If anyone has any tips on how to implement this in LabView 8.2, it would be greatly appreciated!

 

Thanks

0 Kudos
Message 1 of 14
(14,126 Views)

Go to help in the toolbar. Then select "Find Examples" and do a search for Read Binary File.vi. Use this as a template for reading binary files of any type. Remember to use "save as" then saving so you do not overwrite existing files 😉

 



Besides which, my opinion is that Express VIs Carthage must be destroyed deleted
(Sorry no Labview "brag list" so far)
0 Kudos
Message 2 of 14
(14,106 Views)

Hello,

 

thanks for your reply. I found the example you mentioned but it's not really working the way I expected it to. Instead of opening a .bin file, it prompts me for a .dat file. If I force it to choose my .txt file a whole bunch of numbers (not just one and zero) replace the graph which was originally on the front panel.

 

I also tried out the 'open a text file.vi' but that simply displayed a string of the message which is in there.

 

Am I missing something?

 

Thanks

 

 

0 Kudos
Message 3 of 14
(14,093 Views)

What is the format of your text file? I am not very familiar with MATLAB file and text functions, but I suspect that there may be a fairly easy way to get what you want.

 

Lynn 

0 Kudos
Message 4 of 14
(14,083 Views)

Post your file. Then we can have a look at your problem.

 



Besides which, my opinion is that Express VIs Carthage must be destroyed deleted
(Sorry no Labview "brag list" so far)
0 Kudos
Message 5 of 14
(14,079 Views)
Solution
Accepted by topic author sp306

The file extension is irrelevant. Every file is ultimately binary. Even text files. If I understand the objective, I believe one of these two approaches will do what you need:

 

 

This is for a text file that contained the text "ABCDEF"

Message Edited by smercurio_fc on 05-27-2010 02:02 PM
Message 6 of 14
(14,077 Views)

Great! Thanks smercurio_fc!

 

This is exactly what I wanted. Now I'm going to try to understand what you did...

0 Kudos
Message 7 of 14
(14,064 Views)

sp306 wrote:

Great! Thanks smercurio_fc!

 

This is exactly what I wanted. Now I'm going to try to understand what you did...


The first part is just reading the file as an array of U8. These will be the ASCII values of each character. The for-loop processes each element.

 

The top part of the loop converts the ASCII value to a string of zeros and ones. That's done with the Format Into String function. VERY handy function. The output of this is a 1D array of strings. The Concatenate Strings function just "flattens" it into one single string.

 

The bottom part converts it to an array of numeric zeros and ones. This needs to be done in a sequence of steps. First, convert the numeric into an array of Booleans (Number to Boolean Array). Then, convert the Boolean array into an array of zeros and ones (Boolean to (0,1)). Then, reverse the array since LabVIEW orders things in reverse. Since the output is a 2D array I just reshape it into a 1D array using Reshape Array. The Array Size and Multiply Array Elements tell me the size of the 1D array.

0 Kudos
Message 8 of 14
(14,051 Views)

i have to convert the text file into the stream of binary value in matlab ...so how can i convert it ? can i have source code for this conversion???

0 Kudos
Message 9 of 14
(12,948 Views)

What doesn't the code shown do that you need it to do? The source code is right there, so I don't understand what you're asking here.

0 Kudos
Message 10 of 14
(12,938 Views)