MATLAB: Reshape multiple dat files

reshape

Hello all,
I have a large number of dat files in which multiple variables are arranged in a single column one after the other. Each variable is separated by several -9.
Like so: 1 2 3 4 -9 -9 -9 -9 5 6 7 8 -9 -9 -9 -9 9 10 11 12 13 …
Now, I want to reshape them so I have one variable per column and get rid of the -9. Like so:
A B C
1 5 9
2 6 10
3 7 11
4 8 12
Please note that the number of variables, their length and the number of -9 between them may change.
Any ideas ?

Best Answer

One approach:
V = [1 2 3 4 -9 -9 -9 -9 5 6 7 8 -9 -9 -9 -9 9 10 11 12];
Vr = V;
Vr(Vr==-9) = [];
Vr = reshape(Vr(:), [], 3)
VrT = table(Vr(:,1),Vr(:,2),Vr(:,3), 'VariableNames',{'A','B','C'})
Vr =
1 5 9
2 6 10
3 7 11
4 8 12
VrT =
4×3 table
A B C
_ _ __
1 5 9
2 6 10
3 7 11
4 8 12
Related Question