필터 지우기
필터 지우기

Ignore Case when Combining Tables

조회 수: 5 (최근 30일)
Isabelle Museck
Isabelle Museck 2024년 4월 1일
편집: Voss 2024년 4월 1일
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!

채택된 답변

Voss
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
Voss 2024년 4월 1일
편집: Voss 2024년 4월 1일
What function are you using to import the data and how are you calling it? Please share example code.
Voss
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 CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by