Effacer les filtres
Effacer les filtres

re-arrange data

5 vues (au cours des 30 derniers jours)
joseph chahin
joseph chahin le 18 Mar 2021
Commenté : Adam Danz le 24 Mar 2021
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

Réponse acceptée

Adam Danz
Adam Danz le 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)]
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 commentaires
joseph chahin
joseph chahin le 23 Mar 2021
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 le 24 Mar 2021
I'm glad it all worked out without too much of a mess.

Connectez-vous pour commenter.

Plus de réponses (2)

David Hill
David Hill le 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
  1 commentaire
joseph chahin
joseph chahin le 19 Mar 2021
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.

Connectez-vous pour commenter.


dpb
dpb le 18 Mar 2021
Modifié(e) : dpb le 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
>>

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by