필터 지우기
필터 지우기

How do I search for and collect column data based on column name?

조회 수: 8 (최근 30일)
KASSIDY KNUTSON
KASSIDY KNUTSON 2021년 10월 28일
댓글: KASSIDY KNUTSON 2021년 10월 28일
I have a data set of motion capture variables that is 101x2209. Is there a way to search through the column headers, looking for specific variable names, then storing that found column data in a new matrix? (I attached the first file{1,1} for testing)
ie. I want to find all the "LKneVel" columns in my data set and collect them in a new matrix.
name = 'AM';
subject = ["1.1_", "1.2_", "2.1_", "2.2_"];
Pos = ["LHS-LHS", "RHS-RHS"];
for n = 1:4
for k = 1:2
file{n,k} = strcat(name, subject(n), Pos(k), '.txt');
stuff = readmatrix(file{1,1}); % just testing on the first file
S = readlines(file{1,1});
Head = strsplit(S(2,:)); % now we have an array of the headers and their respective locations
[sizeRH sizeCH] = size(Head); % code from here down is my attempt to search and collect
Search{1} = 'LKneVel';
Search{2} = 'LKneMom';
Search{3} = 'LKnePow';
[sizeRH sizeCS] = size(Search);
for i = 1:sizeCH
for j = 1:sizeCS
if string(Head{i}) == Search(j)
index(j) = i;
end
end
end
end
end

채택된 답변

Voss
Voss 2021년 10월 28일
name = 'AM';
subject = ["1.1_", "1.2_", "2.1_", "2.2_"];
Pos = ["LHS-LHS", "RHS-RHS"];
Search = {'LKneVel' 'LKneMom' 'LKnePow'};
n_sub = numel(subject);
n_pos = numel(Pos);
n_search = numel(Search);
file = cell(n_sub,n_pos);
out = cell(n_sub,n_pos,n_search);
for n = 1:n_sub
for k = 1:n_pos
file{n,k} = strcat(name, subject(n), Pos(k), '.xlsx');
[~,~,data] = xlsread(file{n,k});
data(:,1) = [];
data(strcmp(data,'NaN')) = {NaN};
for j = 1:n_search
out{n,k,j} = cell2mat(data(6:end,strcmp(data(2,:),Search{j})));
end
end
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by