Changing data headers in a table (looped)

15 vues (au cours des 30 derniers jours)
Jacob Wagner
Jacob Wagner le 20 Juil 2020
Commenté : Jacob Wagner le 21 Juil 2020
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.

Réponse acceptée

Mohammad Sami
Mohammad Sami le 21 Juil 2020
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');
  1 commentaire
Jacob Wagner
Jacob Wagner le 21 Juil 2020
Thats much easier! Thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by