Filter löschen
Filter löschen

read a file upto certain character

11 Ansichten (letzte 30 Tage)
joy
joy am 15 Jan. 2013
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 Kommentare
José-Luis
José-Luis am 15 Jan. 2013
Bearbeitet: José-Luis am 15 Jan. 2013
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 am 15 Jan. 2013
can u give some commands so that i can understand how fread works in certain way

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

tusu
tusu am 16 Jan. 2013
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

Weitere Antworten (2)

Image Analyst
Image Analyst am 15 Jan. 2013
fread() can do that for a binary file. Is it a binary file or a text file?
  2 Kommentare
joy
joy am 15 Jan. 2013
its a tex file with .txt extension
Image Analyst
Image Analyst am 15 Jan. 2013
Use fgetl() to get a line of text. Then use bin2dec() to turn a chunk of that text into a number.

Melden Sie sich an, um zu kommentieren.


José-Luis
José-Luis am 15 Jan. 2013
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 Kommentare
José-Luis
José-Luis am 16 Jan. 2013
So the ones were characters, not bits.
Image Analyst
Image Analyst am 16 Jan. 2013
That's what I thought, hence my suggestion to use bin2dec().

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Import and Export finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by