Ignore Case when Combining Tables
14 views (last 30 days)
Show older comments
Hello there I am trying to vertivally concatonate tables in my code and some of the varaible names have different a different letter capitliazed in their names when importing that data (for example Accel_Rthigh vs Accel_RThigh). I am getting an error message that all the tables must have the same variable names to be vertically concatonted(although all the variables ARE spelled and Named the same some are capitilized differenetly). Is there a way for MATLAB to ignore the case of the variable names in the table? Or is there a way to easily change the variables with lowecase letters to all match? Or would this be easier if I changed the names of the variables in the importing process? I am looking for any suggstions on the best way to fix this issue without having the change the capitlaztion of the variable names manually in the raw data. Thank you so much!
0 Comments
Accepted Answer
Voss
on 1 Apr 2024
"is there a way to easily change the variables with lowecase letters to all match"
Yes. Use the lower function. Here's an example:
% two tables with one variable each; only difference
% between the variable names is the case:
T1 = table([1;2;3],'VariableNames',{'Accel_Rthigh'});
T2 = table([4;5;6],'VariableNames',{'Accel_RThigh'});
% make all variables in both tables all lowercase:
T1.Properties.VariableNames = lower(T1.Properties.VariableNames);
T2.Properties.VariableNames = lower(T2.Properties.VariableNames);
% vertically concatenate the tables:
T = [T1;T2]
4 Comments
Voss
on 1 Apr 2024
Edited: Voss
on 1 Apr 2024
Here's a way to set the names during import:
opts = detectImportOptions(filename);
opts.VariableNames = lower(opts.VariableNames);
T = readtable(filename,opts)
That's not necessarily less code than changing the names after importing, of course.
Alternatively, if you store all your tables in a single variable, say a cell array or a structure, then you can easily fix all the variable names of all the tables in a loop, e.g.:
T = {T1,T2}; % cell array of tables
for ii = 1:numel(T)
T{ii}.Properties.VariableNames = lower(T{ii}.Properties.VariableNames);
end
If they are in a cell array, then vertically concatenating them all together is easy too:
T_all = vertcat(T{:});
More Answers (0)
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!