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');
  1 件のコメント
Jacob Wagner
Jacob Wagner 2020 年 7 月 21 日
Thats much easier! Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by