# Reshape multiple dat files

1 回表示 (過去 30 日間)
012786534 2017 年 6 月 5 日

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 ?

サインインしてコメントする。

### 採用された回答

Star Strider 2017 年 6 月 5 日
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

サインインしてコメントする。

### その他の回答 (1 件)

Guillaume 2017 年 6 月 5 日

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}]

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeDescriptive Statistics についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by