matlabtextscan

How to read data of a certain length in text file and store in an integer array in matlab


I have a text file that looks like this:

777
3
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000
010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000

The first two lines are the dimensions of the data, for this particular example there are 3 rows of data and 777 columns (or values in each row).

I need to read in this data into Matlab and store the result as an integer array. ie. My array would be 3 rows x 777 columns and look like this:

H = [
1 0 0 0 ...
0 1 0 0 ...
0 0 1 0 ...
]

I'm having problems reading in the data using the specific dimensions and also picking off the first two values from the file. What I did to try and read just the data and not the dimensions was just deleted the first two lines but I would prefer to not do this. I pasted the code I have tried below, I tried two different methods without getting the desired result:

% Method 1
H = textread('myTextFile.txt', '%s');
ncols = size(H, 1);
nrows = size(H{1}, 2);
H = reshape(sscanf([H{:}], '%1d'), ncols,nrows);

% Method 2
fid = fopen('myTextFile.txt', 'r');
H = textscan(fid,'%777s');
fclose(fid);

Solution

  • I would use fopen, feof, fgetl, str2double and two loops, to design a piece of code that suits your specific problem:

    % Open file.
    fid = fopen('myTextFile.txt', 'r');
    % Initialize row index for H.
    a = 1;
    % Initialize number of line counter for file.
    nline = 1;
    % Test for end-of-file.
    while ~feof(fid)
        % Read line from file as string.
        line = fgetl(fid);
        % Test for number of line geater than 2.
        if(nline > 2)
            % Loop through every character from the string.
            % b is column index for H.
            for b = 1:length(line)
                % Extract char from the string, convert it to double and store it in H.
                H(a, b) = str2double(line(b));
            end
            % Increase row index for H.
            a = a + 1;
        end
        % Increase number of line counter.
        nline = nline + 1;
    end
    % Close file.
    fclose(fid);