cannot fill table with chars?

조회 수: 14 (최근 30일)
Budding MATLAB Jockey
Budding MATLAB Jockey 2020년 5월 24일
댓글: Giuseppe Degan Di Dieco 2021년 5월 12일
Hi I am having a pile of issues with the table features in matlab.
I can't figure out how to put a char array into a char variable of a table. I've been reading the table help files but Il'm missing something :(
x=[1;2;3];
y=[4;5;6];
T=table(x,y)
T =
3×2 table
x y
_ _
1 4
2 5
3 6
>> T.words = repmat({''}, 3, 1);
>> T(1,'words') = 'poop';
The number of table variables in an assignment must match.
>> T(1,3) = 'poop';
The number of table variables in an assignment must match.
>> T{1,3} = 'poop';
The value on the right-hand side of the assignment has the wrong width. The assignment requires a value whose width is 1.
  댓글 수: 2
Stephen23
Stephen23 2020년 5월 24일
편집: Stephen23 2020년 5월 24일
"...but Il'm missing something "
It isn't a character array. You defined it as a cell array of empty character vectors:
T.words = repmat({''}, 3, 1);
It is certainly possible to define words to be a character array:
>> T.words = char(nan(3,0))
T =
x y words
_ _ _____
1 4
2 5
3 6
>> T.words(1,1:4) = 'boat'
T =
x y words
_ _ _____
1 4 boat
2 5
3 6
>> size(T.words)
ans =
3 4
but that is likely to be useful only in some specific circumstances. I would imagine that most users would prefer a string array.
Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco 2021년 5월 12일
Dear Stephen,
thanks for your tip, indeed the solution of using a string array is very good.
Best.

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

채택된 답변

Tommy
Tommy 2020년 5월 24일
x=[1;2;3];
y=[4;5;6];
T=table(x,y);
T.words = repmat({''}, 3, 1); % this creates a 3x1 cell array
After these lines, T.words is a cell array:
>> T.words
ans =
3×1 cell array
{0×0 char}
{0×0 char}
{0×0 char}
You can fill it like so:
T(1,'words') = {'a'};
T(2,3) = {'bcd'};
T{3,3} = {'e':'z'};
% alternatively,
T{3,3}{1} = 'e':'z';
Which results in this:
>> T
T =
3×3 table
x y words
_ _ ________________________
1 4 'a'
2 5 'bcd'
3 6 'efghijklmnopqrstuvwxyz'
  댓글 수: 1
Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco 2021년 5월 12일
Dear Tommy,
thanks for your suggestion, it really made me understanding the repmat function.
It helped me in changing the content of a data table, it was a cell array but I defined the substitute as a string.
Best.

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by