# How do I create a table containing a character array if it has only one row?

조회 수: 45 (최근 30일)
Are Mjaavatten 2022년 3월 4일
댓글: Csaba Zoltán Kertész 2022년 3월 13일
This works fine:
Time = [1;2];Force = [12;17];ID = ['ab';'cd'];
T = table(Time,Force,ID)
But this fails:
Time = [1];Force = [12];ID = ['ab'];
T = table(Time,Force,ID)
There may be a good reason for this, but the behaviour does not seem logical to me.
Does anyone have good workaround?
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Stephen23 2022년 3월 4일
"There may be a good reason for this"
Yes: single character vectors are presumed to be part of name-value arguments.

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

### 채택된 답변

Simon Chan 2022년 3월 4일
Time = [1];Force = [12];ID = {'ab'};
T = table(Time,Force,ID)
T = 1×3 table
Time Force ID ____ _____ ______ 1 12 {'ab'}
Actually, the error message tells you the solution.
Time = [1];Force = [12];ID = ['ab'];
T = table(Time,Force,ID)
Error using table (line 282)
Wrong number of arguments.

Caused by:
You might have intended to create a one-row table with the character vector 'ab' as one of its variables. To store text data in a table, use a string array or a cell array of character vectors rather than character arrays. Alternatively, create a cell array with one row, and convert that to a table using CELL2TABLE.
##### 댓글 수: 2없음 표시없음 숨기기
Are Mjaavatten 2022년 3월 4일
I did read that, but I did not quite grasp it. Thank for spoonfeeding it to me.
This seems to work for any number of rows:
T = table(Time,Force,cellstr(ID))
Csaba Zoltán Kertész 2022년 3월 13일
You could also try to use strings instead of character vectors if you do not want to hassle with cells:
Time = [1]; Force = [12]; ID = ["ab"];
T = table(Time, Force, ID)

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

### 카테고리

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

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by