re-arrange data
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Adam Danz
on 18 Mar 2021
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)]
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'})
15 Comments
More Answers (2)
David Hill
on 18 Mar 2021
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
dpb
on 18 Mar 2021
Edited: dpb
on 18 Mar 2021
>> 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
>>
0 Comments
See Also
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!