Keep Variable Name when adding Variables to a table

조회 수: 18 (최근 30일)
Jan Böttner
Jan Böttner 2023년 8월 8일
댓글: Jan Böttner 2023년 8월 8일
Hi!
I have a problem with Variable Names when adding Variables to a table. It seems odd, as it works with the "patients" example from the documentation but not with my own table and Variables. Here is a part of the code:
out.combinednew = addvars(out.combined,out.newvars.(1));
Unable to resolve the name 'out.combined'.
The added Variable gets the name "Var32" instead of "Q_end".
I have also tried writing one column of the table into a variable and then adding that variable to the table but it is the same problem.
Is there another way to keep the Variable Names when adding a new Variable?

채택된 답변

Walter Roberson
Walter Roberson 2023년 8월 8일
The relevant code is using inputname() to try to figure out variable names. inputname() returns empty for any expression giving a value, but returns the variable name in the caller if a plain unindexed variable was passed.
out.newvars.(1) is an expression that pulls out the contents of the first variable of a table, but it is not a plain unindexed variable name, so inputname will return empty.
You should use
out.combinednew = addvars(out.combined,out.newvars.(1), 'NewVariableNames', out.Properties.VariableNames(1));
  댓글 수: 2
Walter Roberson
Walter Roberson 2023년 8월 8일
Note by the way that when you are not using the relative-position properties, that you might as well just do
out.combinednew = [out.combined, out.newvars(:,1)];
Jan Böttner
Jan Böttner 2023년 8월 8일
thanks a lot!

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

추가 답변 (1개)

Florian Bidaud
Florian Bidaud 2023년 8월 8일
Hi
out.combinednew = addvars(out.combined,out.newvars.(1),'NewVariableNames','Q_end');
  댓글 수: 1
Jan Böttner
Jan Böttner 2023년 8월 8일
thanks for your reply, this works but i want the variable name dependend as it might change with different inputs to the code :)

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

카테고리

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

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by