Hello,
i have following problem. I have a 35049x297 table. Now i would like to cut out specific columns with an if command and put these columns in a new table.
VarNames = Daten2.Properties.VariableNames;
for i = 1:columns
if contains(VarNames{1,i},'EN')
newtable{i} = table(Daten2.VarNames{1,i});
end
This is my code but it doesnt work, i dont know why? can someone help me with that? Thanks for the help!!!

댓글 수: 2

Benedikt Skurk
Benedikt Skurk 2021년 5월 26일
Ok i got it..
But the last problem i have is that he doesnt transfer the column headers.
so for example first column header is PE EN Neuruppin_1 and second header is PE EN Wittst_3
U have a solution for that?

댓글을 달려면 로그인하십시오.

 채택된 답변

Scott MacKenzie
Scott MacKenzie 2021년 5월 26일
편집: Scott MacKenzie 2021년 5월 26일

1 개 추천

You have several problems in your code. The assumtion in your question is that you want to create a new table (newtable) containing columns in the existing table (Daten2) where 'EN' appears in the column name. Here's one way to acheive this:
VarNames = Daten2.Properties.VariableNames;
columns = width(Daten2);
j = 1;
for i = 1:columns
if contains(VarNames{i}, 'EN')
newtable(j) = Daten2(:,i);
newtable.Properties.VariableNames{j} = VarNames{i};
j = j + 1;
end
end

댓글 수: 5

Thanks for ur respond!!
My code is now following:
columns = width(Daten2);
j = 1;
for i = 1:columns
if contains(VarNames{i},'EN')
newtable{j} = Daten2{:,i};
j = j + 1;
end
end
but i get as a result for newtable this:
I dont know if that has something to do with the brackets i use or the format of the variables. U have any tips for that?
Benedikt Skurk
Benedikt Skurk 2021년 5월 26일
Ok i got it..
But the last problem i have is that he doesnt transfer the column headers.
so for example first column header is PE EN Neuruppin_1 and second header is PE EN Wittst_3
To transfer the variable names, an extra line is needed...
newtable.Properties.VariableNames{j} = VarNames{i};
I added this line in the answer.
Benedikt Skurk
Benedikt Skurk 2021년 5월 28일
Thanks alot !
Vlatko Milic
Vlatko Milic 2022년 5월 6일
편집: Vlatko Milic 2022년 5월 6일
I have a similar problem but cannot manage to solve it. The table I am working with consists of eight columns.Each column has a unique vairable names, but between position 5 and 10 in the variable names, 2 columns have the same variables. Moreover, I want't to create four new tables (with the two matching columns) based on the original table with 8 columns. I have watched your code for inspiration but cannot manage the code to work according to my wishes, see below for code.
I'm helpful for any advice.
names = table.Properties.VariableNames;
matchVals = {'11111', '22222','33333','44444'}; %variable names i want to match
%
columns=width(table) % 8 columns in total
%%
j=1;
for i=1:columns
if contains(names{i}, matchVals)
newtable(:,j) =table(:,i);
newtable.Properties.VariableNames{j} = names{i};
newtable.Properties.VariableNames{:,j} = names{i};
j = j + 1;
end
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Tables에 대해 자세히 알아보기

태그

질문:

2021년 5월 26일

편집:

2022년 5월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by