Dania
Dania

Reputation: 1688

Reading a text file with no delimiter into a vector MATLAB

I have a text file that has 200 rows, and there are 200 values in every row. The file consists of integers, but they are not separated by any delimiter, not even a space. Here is an example,

1111111111111111111111111111111111111111122222222222222222222222222220000111
1111111111111111111111111111100000000003123333333333333333333333333333300002
0000000000022222222222222222222222222222222211111121212222222222222222111111

The file may contain some strings at the beginning, but I want only to read these numbers. I want to be able to count the occurrence of every integer. So, I will read all these numbers into a vector, or a matrix, where every element in the vector is a number in the file. So, the vector must contain 200 * 200 elements. Then, I will calculate the occurrence of every element.

I checked available file reading methods like textscan, but I think that textscan with this format C = textscan(fid,'%d %d'); requires specifying %d 200 * 200 times, is this the case, or there is a way to use textscan?

I also tried importdata, but when I tried to print the result I didn't get the numeric values. It seems that it only reads the first row, because of this line 200x1 double. Here is the output,

A = 
          data: [200x1 double]
      textdata: {6x1 cell}
    colheaders: {[1x107 char]}

Can you please tell me what method I can use to read the file described above?

Upvotes: 0

Views: 269

Answers (2)

Carl Witthoft
Carl Witthoft

Reputation: 21492

A simple approach: each integer is a separate number (in the desired output), so read the data in line-by-line as a string, then do a loop

for j= 1:numel(a_line_of_integers), 
    x(j) = str2num(a_line_of_integers(j);
end 

And repeat for every row you read in. Note in passing that if you switch to R, x=as.numeric(strsplit(a_line_of_Integers)) is much faster and easier

Upvotes: 1

DaveG
DaveG

Reputation: 753

The data you have with importdata, imports only double values and the headers. You could use the readtable function as follows (I assume 1 header line):

datafile='test.txt';
headerlines=1;

%OPTION1
A=readtable(datafile); %from Matlab R2013b
AA=cell2mat(table2array(A(headerlines+1:end,:)));

%OPTION2
A=textread(datafile,'%s'); %from Matlab R2006a
AA=cell2mat(A(headerlines+1:end,:));

%PROCESSING
b=zeros(size(AA));
for k=1:size(AA,1)
  b(k,:)=str2double(regexp(AA(k,:),'\d','match'));
end

%COUNTING
[nelements,centers]=hist(b',0:9);

The regular expression does the trick of getting out the numbers to columns:

regexp('01112345640','\d','match')

This should return a 1x11 cell with the numbers in char-format.

Upvotes: 1

Related Questions