I have a testfile.txt
, which is a 4 x 4 matrix and tab delimited
1 1 3 4
2 2 3 4
3 1 3 4
4 2 3 4
The output i want is like so:
If it detects that the second column has a 1, insert a new column on the right side, and the new column should contains something like
x=[1 1 0 3]
If it detects that the second column has a 2, insert a new column on the right side, and the new column should contains something like
y=[2 3 4 5]
This is how the output should look like:
1 1 x=[1 1 0 3] 3 4
2 2 y=[2 3 4 5] 3 4
3 1 x=[1 1 0 3] 3 4
4 2 y=[2 3 4 5] 3 4
Ultimately, in MATLAB this is the output I want to get:
1 1 1 1 0 3 3 4
2 2 2 3 4 5 3 4
3 1 1 1 0 3 3 4
4 2 2 3 4 5 3 4
What I've tried is:
test=dlmread('testfile.txt','\t');
m=length(test);
for i=1:m
if find(test(:,2)==1)>0
x=[1 1 0 3];
test=[test(:,1) x test(:,3:4)];
elseif find(test(:,2)==2)>0
y=[2 3 4 5];
test=[test(:,1) y test(:,3:4)];
dlmwrite('testfile.txt',test,'delimiter','\t','precision','%.4f');
end
end
The error I get is the following:
Dimensions of matrices being concatenated are not consistent.
The error is from the following statement:
Error in : test=[test(:,1) x test(:,3:4)]
I'll be really appreciative if someone can help me, since i'm quite new in MATLAB.
Thanks in advance!
[1; 2; 3; 1]
? Can that happen? – Luis Mendo