can i do this program in matlab

hello,
I have a note pad file of lots of bits means 1 & 0 like,1110001111000011111000000111000....Now I want take 8 bit from this bit pattern and store it to another notepad file in space separated like my 2nd notepad file should be like this..
11100011 11000011 11100000 ......like this a
I need this output in decimal is bin2dec be perfect.i need o/p like 23 45 56 in this manner
any idea plz share

 Accepted Answer

Hi joy,
Your question is a bit unclear. It sounds like you are trying to do two different things: 1) You want to write a new text file that inserts spaces between every set of 8 bits, and 2) You want to convert the binary strings into decimals.
First the former-- the easiest way to do this is just to read the text file into Matlab, arrange the matrix into groups of 8, and write a new file. Like this:
txt = fileread('/path/to/your/file.txt'); %read original file to string
n = 8*ceil(length(txt)/8); %number of characters to pad txt variable
txt(end:n)= ' '; %pad to set length to be a multiple of 8
Txt = reshape(txt', 8, [])'; %reshape
Txt(:,end+1)=' '; %add a space after each set of 8 characters
TT = reshape(Txt', 1, []); %convert back to a vector
%now write string to new file
fid = fopen('/path/to/new/file.txt', 'wt');
fprintf(fid, '%s', TT);
fclose(fid);

6 Comments

To do the second way, after doing the reshape, just call bin2dec. Thus, your full code would look like this:
txt = fileread('/path/to/your/file.txt'); %read original file to string
n = 8*ceil(length(txt)/8); %number of characters to pad txt variable
txt(end:n)= ' '; %pad to set length to be a multiple of 8
Txt = reshape(txt', 8, [])'; %reshape
Dec = bin2dec(Txt); %convert to decimal
Dec(:,end+1)=' '; %add a space after each character
TT = reshape(Dec', 1, []); %convert back to a vector
%now write string to new file
fid = fopen('/path/to/new/decfile.txt', 'wt');
fprintf(fid, '%s', TT);
fclose(fid);
joy
joy on 14 Dec 2012
Edited: joy on 14 Dec 2012
hello matt thanx for ans
ur 1st code solve my purpose of separating string in space by taking 8 bit..yes i want that 8 bit separated binary strings into decimal,,,but ur 2nd code not giving me proper O/P..it is giving warning saying that " The argument for the %s format specifier must be of type char (a string)"
actually i am getting the ascii symbol...i.e in 8 bit binary the bit pattern is 11111111 so I expected to get 255 as my O/P but in notepad I am getting ascii symbol of 255 i.e. ÿ
I tried thr program by using %d instead of %s but still it is printing ascii value of space i.e. 32 also bu I need O/P shall be like 255 254 128 this wise
Yep, you're right. That's what I get for not testing my code first! The problem is that Dec is a double matrix after the bin2dec call, not a string. It just needs to be converted to a string. This should do the trick:
txt = fileread('/path/to/your/file.txt'); %read original file to string
n = 8*ceil(length(txt)/8); %number of characters to pad txt variable
txt(end:n)= ' '; %pad to set length to be a multiple of 8
Txt = reshape(txt', 8, [])'; %reshape
Dec = num2str(bin2dec(Txt)); %convert to decimal string
Dec(:,end+1)=' '; %add a space after each character
TT = reshape(Dec', 1, []); %convert back to a vector
%now write string to new file
fid = fopen('/path/to/new/decfile.txt', 'wt');
fprintf(fid, '%s', TT);
fclose(fid);
thank you,,,got ur logic
but sometime i am getting this error,what could be the reason?
??? Error using ==> bin2dec at 54 Binary string may consist only of characters 0 and 1
Error in ==> method_1_3D_2D_plot_stats_modified at 46 Dec = num2str(bin2dec(Txt)); %convert to decimal string

Sign in to comment.

More Answers (0)

Categories

Tags

No tags entered yet.

Asked:

joy
on 14 Dec 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!