How do I search for and collect column data based on column name?
8 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!