assign same name to multiple rows in table

조회 수: 28 (최근 30일)
Mubashir
Mubashir 2017년 9월 8일
댓글: DGM 2024년 3월 5일
I have two variables a1 and a2 of same length, and want to assign same name to them. i tried the command below but failed. Also i have other pair of variable and assign different name to them and want to create single table of all data.
t=table(a1,a2,'RowNames',{'d1'})

채택된 답변

Mubashir
Mubashir 2017년 9월 9일
I resolved this issue. thanks for your answer too
specie=repmat('d1',330,1);
A=table(a1,a2,specie);

추가 답변 (1개)

Guillaume
Guillaume 2017년 9월 8일
I'm not sure what you mean by assign same name to them. It sounds like you haven't completely understood the concept of tables.
Note that 'RowNames' assign names to rows of the table, whereas a1 and a2 will end up as columns (variables) of the table. In any case, Matlab does not allow tables to have two rows or variables with the same name. They're not designed for that.
As the documentation of table clearly says, 'Rownames' must be a cell array of character vectors *that are nonempty and distinct' and the numbers of character vectors must equal the number of rows. The same applies to 'VariableNames'.
What you can do instead is create another variable (e.g. called labels) in your table to which you assign your labels. You can even use this labeling column with functions such as rowfun and varfun, with the 'GroupingVariable' option to perform the same operation on each group of rows.
  댓글 수: 2
Mubashir
Mubashir 2017년 9월 9일
Thanks, I understand. a1 and a2 are column variables and belong to same specie data. So i want to give them specie name in next column e.g. 'd1' as mentioned in figure to whole column. data is too long and not possible to assign name individually.
DGM
DGM 2024년 3월 5일
Posted as a comment-as-flag by @Manuel Campos
Hi Guillaume, I agree that having two variables with the same name doesn't make much sense. However, I don't share the same thoughts on the row case. There are innumerable situations where having the same row names is necessary. By the way, MATLAB does allow you to create a table where specific groups of points of the variables have the same row name, by loading the row names in a loop one by one without preallocating them. Since that is not an efficient solution, I created a new table variable with the row names. I Hope MathWorks solves that soon.

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

카테고리

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