MATLAB Answers

Reshape multiple dat files

2 views (last 30 days)
012786534
012786534 on 5 Jun 2017
Edited: Guillaume on 5 Jun 2017
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 ?

  0 Comments

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 5 Jun 2017
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

  0 Comments

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 5 Jun 2017
Edited: Guillaume on 5 Jun 2017
I'm assuming that within the same file, the number of -9 between each variable is constant:
V = [1 2 3 4 -9 -9 -9 -9 5 6 7 8 -9 -9 -9 -9 9 10 11 12]' %column vector as stated
nines = find(V == -9); %location of all nines
startnines = nines(diff([0; nines]) > 1); %start locations of -9 sequences
endnines = nines(diff([nines; Inf]) > 1); %end locations of -9 sequences
Vr = [V; repmat(-9, endnines(1) - startnines(1) + 1, 1)]; %append -9 after last variable
Vr = reshape(Vr, endnines(1), []); %reshape into columns of variables
Vr(startnines(1):endnines(1), :) = [] %delete -9
edit: Actually it's not much more complicated if the number of -9 between each variable is not even constant within the same file:
V = [1 2 3 4 -9 -9 -9 -9 5 6 7 8 -9 -9 -9 -9 9 10 11 12]' %column vector as stated
nines = find(V == -9); %location of all nines
startnines = nines(diff([0; nines]) > 1); %start locations of -9 sequences
endnines = nines(diff([nines; Inf]) > 1); %end locations of -9 sequences
seqlengths = diff([1; reshape([startnines, endnines+1]', [], 1); numel(V)+1]);
Vr = mat2cell(V, seqlengths, 1);
Vr = [Vr{1:2:end}]

  0 Comments

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!