Effacer les filtres
Effacer les filtres

Keep Variable Name when adding Variables to a table

5 vues (au cours des 30 derniers jours)
Jan Böttner
Jan Böttner le 8 Août 2023
Commenté : Jan Böttner le 8 Août 2023
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?

Réponse acceptée

Walter Roberson
Walter Roberson le 8 Août 2023
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 commentaires
Walter Roberson
Walter Roberson le 8 Août 2023
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 le 8 Août 2023
thanks a lot!

Connectez-vous pour commenter.

Plus de réponses (1)

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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by