re-arrange data

1 回表示 (過去 30 日間)
joseph chahin
joseph chahin 2021 年 3 月 18 日
コメント済み: Adam Danz 2021 年 3 月 24 日
Hello,
I apprecite yuor help for re-arranging the following data.
input (output from table out=readtable('file.xls','PreserveVariableNames',true) )
{'A'} {'B1'} {'X6(1.4M), X15(3M), X25(5M), X50(10M), X75(15M), X100(20M)'} {' X6~0, X15~1, X25~2, X50~3, X75~4, X100~5 '}
appriciated to get the following output matrix:
{'A'} {'B1'} {'X6(1.4M)'} 0
{'A'} {'B1'} {'X15(3M) '} 1
{'A'} {'B1'} {'X25(5M) '} 2
{'A'} {'B1'} {'X50(10M) '} 3
{'A'} {'B1'} {'X75(15M) '} 4
{'A'} {'B1'} {'X100(20M) '} 5
{'X6(1.4M)'} from element 3 before ccomma,
0, 1,....5 field 4 after ~
Thank you,
Br..
Joseph

採用された回答

Adam Danz
Adam Danz 2021 年 3 月 18 日
It looks like you want the output to be a cell array.
out = {'A', 'B1', 'X6(1.4M), X15(3M), X25(5M), X50(10M), X75(15M), X100(20M)', ' X6~0, X15~1, X25~2, X50~3, X75~4, X100~5 '};
x = strtrim(strsplit(out{3},','))';
n = str2double(regexp(strtrim(strsplit(out{end},',')), '\d+$','match','once'))';
M = [repmat(out(1:2),numel(x),1), x, num2cell(n)]
M = 6×4 cell array
{'A'} {'B1'} {'X6(1.4M)' } {[0]} {'A'} {'B1'} {'X15(3M)' } {[1]} {'A'} {'B1'} {'X25(5M)' } {[2]} {'A'} {'B1'} {'X50(10M)' } {[3]} {'A'} {'B1'} {'X75(15M)' } {[4]} {'A'} {'B1'} {'X100(20M)'} {[5]}
Alternatively, a table,
T = table(string(repmat(out(1),numel(x),1)), ...
string(repmat(out(2),numel(x),1)),...
string(x), ...
n, 'VariableNames', {'A','B','X','n'})
T = 6×4 table
A B X n ___ ____ ___________ _ "A" "B1" "X6(1.4M)" 0 "A" "B1" "X15(3M)" 1 "A" "B1" "X25(5M)" 2 "A" "B1" "X50(10M)" 3 "A" "B1" "X75(15M)" 4 "A" "B1" "X100(20M)" 5
  15 件のコメント
joseph chahin
joseph chahin 2021 年 3 月 23 日
Those cases occur many times. I meant a few cases but are repeated in teh data. it is a large set of data. this was just an example. Anyway, i have solved with an easy manner. Please enclosed my script which it gives the desired output:
H_out =
{'A'} {'a0'} {'a_0'} {[0]} {'On' } {'1' }
{'A'} {'a0'} {'a_1'} {[1]} {'Off'} {'0' }
{'A'} {'a0'} {'a_2'} {[2]} {'Off'} {'0' }
{'B'} {'b0'} {'b_0'} {[0]} {'Off'} {'None'}
{'B'} {'b0'} {'b_1'} {[1]} {'Off'} {'None'}
{'B'} {'b0'} {'b_2'} {[2]} {'Off'} {'None'}
{'B'} {'b0'} {'b_3'} {[3]} {'Off'} {'None'}
{'C'} {'c0'} {'c_0'} {[0]} {'On' } {'On' }
{'C'} {'c0'} {'c_1'} {[1]} {'Off'} {'Off' }
{'C'} {'c0'} {'c_3'} {[2]} {'Off'} {'Off' }
{'C'} {'c0'} {'c_4'} {[4]} {'Off'} {'-' }
{'D'} {'d0'} {'d_0'} {[0]} {'Off'} {'Off' }
{'D'} {'d0'} {'d_1'} {[1]} {'Off'} {'Off' }
Adam Danz
Adam Danz 2021 年 3 月 24 日
I'm glad it all worked out without too much of a mess.

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

その他の回答 (2 件)

David Hill
David Hill 2021 年 3 月 18 日
Output will have to be a cell array.
Input= {'A','B1','X6(1.4M), X15(3M), X25(5M), X50(10M), X75(15M), X100(20M)',' X6~0, X15~1, X25~2, X50~3, X75~4, X100~5 '};
a=regexp(Input{3},'[X()0-9.M]+(?=,)','match');
b=regexp(Input{4},'(?<=~)\d+','match');
for k=1:length(a)
Output{k,1}=Input{1};
Output{k,2}=Input{2};
Output{k,3}=a{k};
Output{k,4}=b{k};
end
  1 件のコメント
joseph chahin
joseph chahin 2021 年 3 月 19 日
Best David, thank you for the reaction. I tried you script as well. still not get the output. Please see down my reaction to Adam. Thx. Br. Joseph.

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


dpb
dpb 2021 年 3 月 18 日
編集済み: dpb 2021 年 3 月 18 日
>> C=[{'A'}, {'B1'}, {'X6(1.4M), X15(3M), X25(5M), X50(10M), X75(15M), X100(20M)'}, {' X6~0, X15~1, X25~2, X50~3, X75~4, X100~5 '} ]
C =
1×4 cell array
{'A'} {'B1'} {'X6(1.4M), X15(3M), X25(5M), X50(10M), X75…'} {' X6~0, X15~1, X25~2, X50~3, X75~4, X100~5 '}
>> split(C{3},',')
ans =
6×1 cell array
{'X6(1.4M)' }
{' X15(3M)' }
{' X25(5M)' }
{' X50(10M)' }
{' X75(15M)' }
{' X100(20M)'}
>> str2double(extractAfter(split(C{4},','),'~'))
ans =
0
1
2
3
4
5
>>

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by