How to ensure that all the characters in a file are ones...

Asked by Travis on 10 Jul 2012
Latest activity Commented on by Travis on 10 Jul 2012

I have a file that is around 250MB, and i am pretty sure that it is all ones, but i need to write a matlab script that will check this and is quick.

As far as I know, the file is one line as well:

sample: 11111111111111111111111111111111111.....

I tried the following:

fid=fopen(filename);

kk=0;

while ~feof(fid)

    ONEs=textscan(fid,'%c',1000);
    ONEs=cell2mat(ONEs);
    if ~isequal(ONEs,num2str(ones(1000,1)))
        ONEs
        progress{2,kk}='Found it'
    end
    kk=kk+1
    progress(1,kk)=kk;
end

and it works, but it will take days to find if there are any other characters than ones. If you have any ideas that would be great. Thanks!

0 Comments

Travis

Products

1 Answer

Answer by Jan Simon on 10 Jul 2012
Edited by Jan Simon on 10 Jul 2012
Accepted answer

textscan and cell2mat waste a lot of time. Better import the data in binary format directly. Here I use UINT8, but a CHAR would work sufficiently also. You can try to increase the block size to 10'000.

fid = fopen(FileName)
match = uint8(1);
found = [];
while ~feof(fid)
  data = fread(fid, 1000, '*uint8');
  if any(data ~= match)
    disp('Non 1 found.');
    found(end + 1) = ftell(fid);
  end
end

1 Comment

Travis on 10 Jul 2012

quick question...

How would i be able to easily display all of the characters that aren't ones?

Jan Simon

Contact us