How to put string to a column of a table
조회 수: 116 (최근 30일)
이전 댓글 표시
Hi,
It may sound silly, but I cant figure out how to insert a string to a column of a table.
I have tried few ways to insert, but failed.
The thing is, I need to insert a string, suppose, "cricketer" in the column, named "Role" in the table, called, "Players".
The table is of form like this:
Name Role
"abc"
"bcd"
"cdf"
I have tried few things randomly, like
Players.Role(:,1)="cricketer"
but it appears NaN in the cells of the Role column.
Can you please show me the right way to do it?
thanks,
댓글 수: 0
답변 (1개)
Adam Danz
2020년 7월 7일
편집: Adam Danz
2020년 7월 13일
"... but it appears NaN in the cells of the Role column."
Sounds like the Role column is an empty cell array or a numeric column. Table columns but contain the same type of data and cannot contain mixed classes. You can use a cell array to enter mixed data types if needed.
Players = table(["abc";"dcd";"cdf"], cell(3,1),'VariableNames',{'Name','Role'})
Players.Role{1} = 'Cricketer';
If the entire column should be strings, you can convert the cell array to a string array,
Players.Role = repmat("Cricketer", size(Players.Name))
or you can set the initial values of "Role" as a string array rather than a cell array,
Players = table(["abc";"dcd";"cdf"], ["";"";""],'VariableNames',{'Name','Role'})
Players.Role(:) = "Cricketer";
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!