Renaming headers in Table
49 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jake Bowd
el 16 de Jul. de 2020
Comentada: Fangjun Jiang
el 16 de Jul. de 2020
Hi,
I want to make a table from an array by using the array2table function. This works perfectly fine for my datasets whereby table headers do not match. But on occassion I have it whereby the data will have headers that are the same. I.e.
'ground_force_px' 'ground_force_py' 'ground_force_pz' 'ground_force_vx' 'ground_force_vy' 'ground_force_vz'
'ground_force_px' 'ground_force_py' 'ground_force_pz' 'ground_force_vx' 'ground_force_vy' 'ground_force_vz'
Is there a way whereby I can to convert from array2table whilst keeping these header names the same ?
Many thanks,
5 comentarios
Fangjun Jiang
el 16 de Jul. de 2020
See note under Steven's answer. "duplicated headers" is the keyword here. Look back your question, you will see why three MVPs couldn't guess it right.
Respuesta aceptada
Steven Lord
el 16 de Jul. de 2020
I'm not sure I see the problem.
A = magic(6)
V = ["ground_force_px", "ground_force_py", "ground_force_pz", ...
"ground_force_vx", "ground_force_vy", "ground_force_vz"];
T = array2table(A, 'VariableNames', V)
If you mean you want to have a table where two of the variable names are the same, that's not allowed.
V2 = V;
V2(6) = V2(2);
T2 = array2table(A, 'VariableNames', V2) % Will error
7 comentarios
Steven Lord
el 16 de Jul. de 2020
As of release R2019b table variable names don't have to be valid MATLAB identifiers. This means table arrays created with variable names that have gone through genvarname are not necessarily the same as one whose names have gone through matlab.lang.makeUniqueStrings.
A = magic(3);
V = {'hocus pocus', 'abracadabra', 'hocus pocus'};
G = genvarname(V);
T1 = array2table(A, 'VariableNames', G)
U = matlab.lang.makeUniqueStrings(V);
T2 = array2table(A, 'VariableNames', U)
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!