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 CenterFile Exchange 中查找有关 Entering Commands 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by