Reputation: 12452
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?
Upvotes: 2
Views: 427
Reputation: 885
In case your data is numerical, you may try using
twopts = importdata('input.txt');
ones_row = ones(1,20);
twopts = [twopts; ones_row];
Upvotes: 0
Reputation: 11810
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];
Upvotes: 0
Reputation: 14251
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]
Upvotes: 2