Adding a column of the same character name for all the rows in a table
22 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Aaron Devanathan
el 9 de Abr. de 2021
Respondida: Peter Perkins
el 3 de Mzo. de 2022
I have a table in MATLAB that I'd like to add a column that contains a character variable that's the same throughout the column, but different size lengths. Here's a code for two random tables.
Is there a way, for example, for me to add a column named 'Time' that contains the character 'hours' for both based on the number of rows of the table?
r1 = rand(100,1);
r1 = array2table(r1);
r1.Properties.VariableNames = {'Random1'};
r2 = rand(54,1);
r2 = array2table(r2);
r2.Properties.VariableNames = {'Random2'};
1 comentario
Abhishek Gupta
el 12 de Abr. de 2021
Hi,
As per my understanding, you want to add a column to the existing table. You can do the same in the following way: -
r1 = rand(100,1);
r1 = array2table(r1);
r1.Properties.VariableNames = {'Random1'};
strs1 = repmat('hours', size(r1, 1), 1);
vTbl1 = table(strs1, 'VariableNames',{'Time'});
r1 = [r1, vTbl1];
r2 = rand(54,1);
r2 = array2table(r2);
r2.Properties.VariableNames = {'Random2'};
strs2 = repmat('hours', size(r2, 1), 1);
vTbl2 = table(strs2, 'VariableNames',{'Time'});
r2 = [r2, vTbl2];
Respuesta aceptada
Scott MacKenzie
el 13 de Mayo de 2021
From my understanding of the question, this is simply a matter of making the necessary assignment to r1.Time or r2.Time. Do so and a new column with variable name Time is automatically appended to the table:
r1 = rand(100,1);
r1 = array2table(r1);
r1.Properties.VariableNames = {'Random1'};
r1.Time = repmat({'hours'}, height(r1), 1);
r2 = rand(54,1);
r2 = array2table(r2);
r2.Properties.VariableNames = {'Random2'};
r2.Time = repmat({'hours'}, height(r2), 1);
Here's a command-window dump of the first 8 rows in the new r1:
>> head(r1)
ans =
8×2 table
Random1 Time
_________ _________
0.49707 {'hours'}
0.32079 {'hours'}
0.61628 {'hours'}
0.0068941 {'hours'}
0.062934 {'hours'}
0.92002 {'hours'}
0.90616 {'hours'}
0.83154 {'hours'}
0 comentarios
Más respuestas (1)
Peter Perkins
el 3 de Mzo. de 2022
It's even simpler than that:
>> r1 = array2table(rand(5,1),'VariableNames',"Random1");
>> r1.Time(:) = "hours"
r1 =
5×2 table
Random1 Time
_______ _______
0.31907 "hours"
0.98605 "hours"
0.71818 "hours"
0.41318 "hours"
0.09863 "hours"
It's not clear what you would do with this variable, though. I'm gonna take a guess that at some point you will end up vertically concatenating these tables and the text will be different in different tables (except right now, you can't because your var names are different). In that case, use categorical, not text.
>> r1 = array2table(rand(2,1),'VariableNames',"X");
>> r1.Time(:) = categorical("hours")
r1 =
2×2 table
X Time
_______ _____
0.73456 hours
0.63731 hours
>> r2 = array2table(rand(3,1),'VariableNames',"X");
>> r2.Time(:) = categorical("days")
r2 =
3×2 table
X Time
________ ____
0.073842 days
0.12051 days
0.9816 days
>> [r1; r2]
ans =
5×2 table
X Time
________ _____
0.73456 hours
0.63731 hours
0.073842 days
0.12051 days
0.9816 days
0 comentarios
Ver también
Categorías
Más información sobre Data Type Conversion 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!