read a file upto certain character

Hello I have a file which contains 1342400 bits means 10101010010......like this way,
I need to read this file upto 1341440 bits ,how to do so?
after this I need to take chunk of 8bit from this bit sequence and have to convert it in decimal value,separated by ,
like 11111111111111111111111.... i need to convert it in 255,255,255 this way
any help for my two queries

댓글 수: 2

José-Luis
José-Luis 2013년 1월 15일
편집: José-Luis 2013년 1월 15일
fread will do that for you. You could read your data as uint8, and avoid transforming between binary and decimal. Is this homework?
joy
joy 2013년 1월 15일
can u give some commands so that i can understand how fread works in certain way

댓글을 달려면 로그인하십시오.

 채택된 답변

tusu
tusu 2013년 1월 16일

0 개 추천

Joy
What u can do is...say ur given data set is in a file name test.txt where it has 26 characters..
do the following
txt=filered(test.txt);% now txt variable has all ur 26 chars,but u need first 16
so
new_txt=txt(1:16);
simple

추가 답변 (2개)

Image Analyst
Image Analyst 2013년 1월 15일

0 개 추천

fread() can do that for a binary file. Is it a binary file or a text file?

댓글 수: 2

joy
joy 2013년 1월 15일
its a tex file with .txt extension
Image Analyst
Image Analyst 2013년 1월 15일
Use fgetl() to get a line of text. Then use bin2dec() to turn a chunk of that text into a number.

댓글을 달려면 로그인하십시오.

José-Luis
José-Luis 2013년 1월 15일

0 개 추천

The documentation has examples that illustrate how fread() works. Here is a (rather pointless) example of how to read a file bit by bit and then transforming those bits into uint8, which is what you are trying to achieve, if I understood your question right. I insist that it is much better to read directly as uint8
fid = fopen('your_file.txt','r');
numBits = 800; %multiple of 8, in this case it means the first 100 characters
your_vals = reshape(fread(fid,800,'ubit1'),[],8);
fclose(fid);
your_ascii_number = sum(bsxfun(@times,your_vals,2.^(7:-1:0)),2);

댓글 수: 5

joy
joy 2013년 1월 15일
thanx for reply Jose,but sorry I could not make my question and my goal clear to you...I am trying to do it once again...take small example...
I have a notepad file stating 11110000111100111111111101 it has totals 26 characters..but I need to read only 16 from it so that shall be 1111000011110011..and now I want to convert it to decimal like it output must be like 240,243 which can be done easily
my main query is how to read only those 16 characters from out of total 26 characters from this text file...I don want to read the whole file..I want to extract first 16 characters and want to do rest of my program that where I stuck
regards Joy
José-Luis
José-Luis 2013년 1월 15일
편집: José-Luis 2013년 1월 15일
The code i gave you does exactly that. Try replacing 800 by 16 and giving it the right file name. Or do you mean that the 0s and 1s are strings in the text file?
I tried the code with above sample code with my given data set of 26 characters i.e. 11110000111100111111111101
but I am getting O/P
your_vals =
1 0 1 0 1 0 1 0
0 0 1 0 0 0 1 0
I need first 16 chars...but getting something else
José-Luis
José-Luis 2013년 1월 16일
So the ones were characters, not bits.
Image Analyst
Image Analyst 2013년 1월 16일
That's what I thought, hence my suggestion to use bin2dec().

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

질문:

joy
2013년 1월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by