필터 지우기
필터 지우기

Change Variable Names of a Table

조회 수: 15 (최근 30일)
Tobias Pfeifer
Tobias Pfeifer 2021년 3월 17일
댓글: Steven Lord 2021년 3월 17일
Hi,
i have a Table, that consists of temperature data measured with a specific amount of sensors. Now i want to change the Variable Names of the columns with the sensor data to Sensor 1.......Sensor n. The first 3 columns should not be renamed. I want to create a code that gets the number of the sensors and renames them itself. This code should be usable for different measurement without the need to rewrite the code. Thanks in advance!

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 3월 17일
Variable names exist in the properties of the table.
tblName.Properties.VariableNames
Use indexing to set just the names you want. For example, consider the folowing code for renaming 5 sensors starting at variable 4.
% Create table of 5 rows, 10 variables
A=array2table(rand(5,10))
A = 5×10 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 ________ ________ ________ _______ _______ _______ _________ ________ _______ _______ 0.087249 0.9872 0.10918 0.73819 0.29765 0.15232 0.79646 0.40895 0.19119 0.71244 0.42075 0.071006 0.95593 0.76589 0.78594 0.86312 0.13858 0.23522 0.94558 0.59231 0.5377 0.70569 0.011251 0.44339 0.3339 0.54552 0.0070234 0.40979 0.39547 0.29689 0.18404 0.86838 0.76226 0.86665 0.25129 0.8476 0.060897 0.070054 0.14161 0.27797 0.11394 0.1903 0.54479 0.14489 0.99445 0.91203 0.077083 0.73749 0.91161 0.5621
% Create variable names for 5 sensors
n=5;
sNames = "Sensor " + (1:n);
% rename variables starting at Var4, and including n continguous variables.
A.Properties.VariableNames(4:4+n-1)=sNames
A = 5×10 table
Var1 Var2 Var3 Sensor 1 Sensor 2 Sensor 3 Sensor 4 Sensor 5 Var9 Var10 ________ ________ ________ ________ ________ ________ _________ ________ _______ _______ 0.087249 0.9872 0.10918 0.73819 0.29765 0.15232 0.79646 0.40895 0.19119 0.71244 0.42075 0.071006 0.95593 0.76589 0.78594 0.86312 0.13858 0.23522 0.94558 0.59231 0.5377 0.70569 0.011251 0.44339 0.3339 0.54552 0.0070234 0.40979 0.39547 0.29689 0.18404 0.86838 0.76226 0.86665 0.25129 0.8476 0.060897 0.070054 0.14161 0.27797 0.11394 0.1903 0.54479 0.14489 0.99445 0.91203 0.077083 0.73749 0.91161 0.5621
  댓글 수: 2
Tobias Pfeifer
Tobias Pfeifer 2021년 3월 17일
Thank you very much for the quick response!
Steven Lord
Steven Lord 2021년 3월 17일
As of release R2020a there's a function named renamevars for renaming table and timetable variables.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by