CroCo
CroCo

Reputation: 5741

how to load txt contains different columns with different rows

I have txt that contains different columns. For example, the following txt has

1 2
1 2
1 2
1 2
1 2
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

the first column is ones, therefore I'm expecting the output to be for the first column is ones. When I use load, it throws out this error

Error using load
Number of columns on line 6 of ASCII file test.txt must be the same as previous lines.

I used importdata but the output is

 1
 1
 1
 1
 1
 1
 3
 1
 3
 1
 3
 1
 3

Upvotes: 1

Views: 995

Answers (4)

CitizenInsane
CitizenInsane

Reputation: 4855

Assuming your file contains only numeric values, you can parse it into cell array like this:

% Read full file
str = fileread('A.txt'); 

% Convert text in a cell array of strings  
c = strsplit(str, '\n');

% Convert 'string' elements to 'double' 
n = cellfun(@str2num, c, 'UniformOutput', false);

You can then access individual lines (as an array of double) like this:

>> n{1}

ans =

     1     2

>> n{7}

ans =

     1     2     3    4

PS: This is a duplicate of my answer to How to load a text file in Matlab when the number of values in every line are different

Upvotes: 1

Setsu
Setsu

Reputation: 1228

csvread for some reason gives different behavior between R2014a and R2014b with no documentation for the change. Use dlmread instead:

dlmread('input.txt')

Output gives:

ans =
     1     2     0     0
     1     2     0     0
     1     2     0     0
     1     2     0     0
     1     2     0     0
     1     2     0     0
     1     2     0     0
     1     2     0     0
     1     2     0     0
     1     2     0     0
     1     2     0     0
     1     2     3     4
     1     2     3     4
     1     2     3     4
     1     2     3     4
     1     2     3     4

Upvotes: 2

Mikhail Genkin
Mikhail Genkin

Reputation: 3460

May be this is not the best approach, but it is still short. If your file contains less then 1000000 lines,you can use:

a=importdata('1.txt','\n',1000000)

As a result, a will be cell array, with number of cells equal to number of you lines (rows). Now, say, if you want to obtain 3rd line, just use:

b=str2num(a{3})

b is a row vector - the third line in your txt file.

Edit: if you want to extract the 1st column, use for loop:

a=importdata('1.txt','\n',1000000);
b=zeros(1,numel(a));  % b will be first column
for i=1:numel(a)       % loop throw rows 
    p=str2num(a{i});   % obtain row #i
    b(i)=p(1);            % b(i)=first element
end

May be there is a better way. This solution is convenient for working with rows, I didn't notice, that you want to obtain column

Upvotes: 2

MattyB
MattyB

Reputation: 952

In Matlab R2014b

c=csvread('test.txt')

c =

     1     2     0     0
     1     2     0     0
     1     2     0     0
     1     2     0     0
     1     2     0     0
     1     2     3     4
     1     2     3     4
     1     2     3     4
     1     2     3     4

Upvotes: 2

Related Questions