Concatenating two tables with different array lengths in a variable of type 'char'

조회 수: 22 (최근 30일)
Hi,
I have 2 tables x and y. x and y are tables with same variable names and same data types. There is a variable called 'Filename' in both, which is a char variable which may or may not be of the same length. I would like to concatenate them vertically and I try
y = [y ; x];
It works fine when the length of 'Fiilename' is the same in x and y but if not, I get an error message:
Could not concatenate the table variable 'Filename' using VERTCAT.
Caused by:
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
A summary of both the tables gives:
summary (x)
Variables:
Filename: 1×22 char
summary (y)
Variables:
Filename: 1×23 char
I have not shown the results of other variables because it gets lengthy here and they are all the same, differing only in some numeric values.
Also it makes me wonder if it's not possible to have strings of different length in a Table variable in different rows as I feel it would be a very common need.
I think I must be missing something very silly and easy but can't seem to find it.
How do I overcome this? Thank you!

채택된 답변

dpb
dpb 2019년 5월 27일
"it makes me wonder if it's not possible to have strings of different length in a Table variable in different rows"
Just like a a character array, no, a table variable must be a regular array which means character string data must be the same length.
But, you can do one of two things, use cellstr() to create the variable in the table as cell strings or the new(ish) strings class.
x.Filename=string(x.Filename);
y.Filename=string(y.Filename);
x=[x;y];
  댓글 수: 1
Chandramouli Santhanam
Chandramouli Santhanam 2019년 5월 27일
편집: Chandramouli Santhanam 2019년 5월 27일
Thanks a lot! I understood about handling text in Table variables and the difference between a 'Character' and a "String" :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by