Add Numbers to the End of a Table

조회 수: 8 (최근 30일)
Spencer Ferris
Spencer Ferris 2021년 6월 1일
답변: Adam Danz 2021년 6월 2일
I have a table, sampled_x_coord_table. I also have a value, participant_number, that corresponds to a number 1-17, the participant_number is set at the top of my script.
I want to add the participant number to the end of the name of the table. I feel like there's a really obvious solution that I am not seeing.
Thank you!
Spencer
  댓글 수: 2
Adam Danz
Adam Danz 2021년 6월 1일
> I want to add the participant number to the end of the name of the table
It's unclear whether you want to add a new row to the table or add the number to the end of an existing cell within the table. Both interpretations require more information such as how you'd like to fill in the other cells if you're adding a new row. I picture is worth 1000 words.
Spencer Ferris
Spencer Ferris 2021년 6월 2일
Ah sorry, I see how that wasn't clear. I mean that I want to add the participant number to the variable name of the table. For example if the participant number is 10, I want to make it so that the name of the table is sample_x_coord_table_10.

댓글을 달려면 로그인하십시오.

답변 (2개)

Star Strider
Star Strider 2021년 6월 2일
Possibly —
T1 = table(rand(5,1),rand(5,1), 'VariableNames',{'sample_x_coord_table','sample_y_coord_table'})
T1 = 5×2 table
sample_x_coord_table sample_y_coord_table ____________________ ____________________ 0.59047 0.29522 0.1319 0.62087 0.1479 0.91926 0.44163 0.04417 0.99606 0.49544
VN = T1.Properties.VariableNames;
compfcn = @(n) compose(['%s_',num2str(n)],string(VN));
ParticipantNr = 10;
T1.Properties.VariableNames = compfcn(ParticipantNr)
T1 = 5×2 table
sample_x_coord_table_10 sample_y_coord_table_10 _______________________ _______________________ 0.59047 0.29522 0.1319 0.62087 0.1479 0.91926 0.44163 0.04417 0.99606 0.49544
.

Adam Danz
Adam Danz 2021년 6월 2일
> if the participant number is 10, I want to make it so that the name of the table is sample_x_coord_table_10.
Your describing dynamic variable naming and this is bad idea ( why? ).
Instead, you can store the participant number in the table's description property.
sample_x_coord_table = table(__);
sample_x_coord_table.Properties.Description = 'participant 10';
Or combine the tables into a cell array and use the index as participant number
Data{10} = sample_x_coord_table;
You could also add a participant number column to all tables so the tables can be concatenated.

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by