Changing data headers in a table (looped)

조회 수: 15 (최근 30일)
Jacob Wagner
Jacob Wagner 2020년 7월 20일
댓글: Jacob Wagner 2020년 7월 21일
I have a large data file gathering information from some test equipment in the field. Two pieces of equipment a generating more or less the same headers, ultimately i am trying to sync the two tables so I can plot time stamped data from both. As part of this, I want to rename some of the variables in the table, and usually they occur sequentially.
For example:
FullBR_1_ FullBR_2_ FullBR_3_ and so on up to 8. I would like to change the names to something else, but maintain the numbering. ie:
EFB_1 EFB_2 EFB_3
I have read a bunch of answer in here that point out why creating variables should not be done dynamically, however I think I am just trying to change the dynamic field names instead? My code is as follows:
close all
clear
e_table = readtable('CSM.dat');
s_table = readtable('CSS.dat');
for i=1:8
var_name = ['FullBR_' num2str(i) '_']
nvar_name = ['EFB_' num2str(i)]
e_table.Properites.VariableNames{var_name} = nvar_name;
end
I have a feeling this is due to my own ignorance on what the proper way to store the data is, so any advice would be apprecaited. I have skimmed this: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval#answer_236124
But it is kind of information overload.

채택된 답변

Mohammad Sami
Mohammad Sami 2020년 7월 21일
What is discouraged is dynamic evaluation of a commands which is a security risk. (Risk of code injection)
Dynamic referencing of table columns or structure fields in matlab is fine.
You can rename the table column names by changing the table.Properties.VariableNames as you were trying to do above.
Assuming the pattern you mentioned, we can use regex to match and replace the pattern.
e_table.Properties.VariableNames = regexprep(e_table.Properties.VariableNames,'^(FullBR_)(\d+)_$','EFB_$2');

추가 답변 (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