I have a cell array of VariableNames that Matlab created when reading a csv file. I want to change them to my own choice of names however, the order of the names is not guaranteed.
iddata = readtable('data.csv');var_names = iddata.Properties.VariableNames;names_to_replace = {'TonnageDeBSA', 'D__bitD___eauAuBSA', ... 'PressionPalierSAG', 'PuissanceSAG'};replacements = {'TonnageBSA', 'DebitEauBSA', ... 'PressionPalierSAG', 'PuissanceSAG'};replace_map = containers.Map(names_to_replace, replacements);
Obviously I could use a for loop:
new_var_names = {};for i=1:numel(var_names) new_var_names(i) = {replace_map(var_names{i})};end
But I am hoping there is an easier way.
Does MATLAB have anything like a list comprehension in Python?
new_var_names = {replace_map[name] for name in var_names}
Even better if there is a way to handle failed matches:
new_var_names = {replace_map[name] for name in var_names if name in replace_map}
Best Answer