Ignore Case when Combining Tables
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Voss
2024-4-1
"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 个评论
Voss
2024-4-1
编辑:Voss
2024-4-1
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{:});
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!