Renaming headers in Table

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 Comments

not clear
Hi Fangjun,
Apologies for not being clear.
When try to use array2table I recieve the following error message:
Error using array2table (line 73)
Duplicate table variable name: 'ground_force_px'.
I know this is as there are two headers with the same name and so cannot be achieved.
Is there a way to identify when there is a duplicate of a header and then add a '1' infront of the first one and a '2' in front of the second occurance so that there are duplicates ?
Many thanks
still not clear. Best way to do for this question
  1. Show small example data
  2. Show the code and data that works for you
  3. Show the code and data that has the error
My structure looks like:
data textdata colheaders
1x290 cell 13x290 cell 295x290 double
I wish to make a table using 'data' for the data and 'colheaders' for the table headers.
I have a problem. There are repetitions in 'colheaders', which are:
'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'
and I receive the following error message:
Duplicate table variable name: 'ground_force_px'.
Question: Can I either delete the colheaders that repeat themselves along with the corresponding data, or can I rename the any colheaders that repeat?
The reason why most work is that there are no repeats of 'colheaders'. However for this particular visit, there are repeats. As such preventing from creating a table.
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.

Sign in to comment.

 Accepted Answer

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 Comments

Hi Steven,
The problem arises when I'm trying to make a Table from a 295x290 double matrix (data) and a 1x290 cell (col.headers) as some 'col.headers' are repeated.
When researching I soon realised that you cannot repeat column headers in a table. So I was wondering if there is a 'get-around' to solve this.
Is there a way whereby I can identify which columns repeat in the col.headers and then delete the data accordingly in the matrix ?
Best wishes,
%%
col.headers={'a','d','c','b','b','d'};
data=magic(6);
[Headers,index]=unique(col.headers);
T=array2table(data(:,index),'VariableNames',Headers)
You can rename the duplicate variable names using matlab.lang.makeUniqueStrings.
A = magic(6)
V2 = ["ground_force_px", "ground_force_py", "ground_force_px", ...
"ground_force_vx", "ground_force_vy", "ground_force_vz"];
T2 = array2table(A, 'VariableNames', V2) % Will throw an error; px appears twice
V2U = matlab.lang.makeUniqueStrings(V2)
T2U = array2table(A, 'VariableNames', V2U)
Hi Steven,
This was exactly what I wanted. I wanted unique headers for the col.headers before then being able to create a table.
Thank you so much!!! Much appreciated.
Best wishes,
Jake
>> genvarname({'a','b','a'})
ans =
1×3 cell array
{'a'} {'b'} {'a1'}
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)
good point! I see the difference.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!