2

I have a matrix 2x20 from a text file
I want to add a row of ones to that matrix

twopts = reshape(textread('input.txt', '%s'),2,20); % 2 by 20 ones_row = ones(1,20); %1 by 20 of ones twopts = [twopts;ones_row] 

Gives me an error:

"Error using vertcat CAT arguments dimensions are not consistent."

But the matrix dimensions match... 2x20 and 1x20 to make 3x20

What's wrong with it and how do I fix it?

1
  • try size(twopts) and size(ones_row) to make sure they are the right size? Commented Oct 13, 2012 at 10:06

3 Answers 3

2

twopts is a cell array of strings and ones_row is a matrix, you can't put these together.

Does this do what you want?

twopts = reshape(textread('input.txt', '%s'),2,20); % 2 by 20 ones_row = ones(1,20); %1 by 20 of ones ones_row = mat2cell(ones_row, 1, ones_row); % convert to cell array twopts = [twopts;ones_row] 

Alternatively, if the input data contains numbers, not text, you might want to convert the cell array to a matrix instead:

twopts = reshape(textread('input.txt', '%s'),2,20); % 2 by 20 twopts = cellfun(@str2num,twopts); ones_row = ones(1,20); %1 by 20 of ones twopts = [twopts;ones_row] 
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of reading strings, as you do now, try to simply read numbers (that is - if your data is numbers). Simply omit the %s parameter to textread:

twopts = textread('input.txt'); ones_row = ones(1,20); twopts = [twopts; ones_row]; 

Comments

0

In case your data is numerical, you may try using

twopts = importdata('input.txt');

ones_row = ones(1,20);

twopts = [twopts; ones_row];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.