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

1 view (last 30 days)
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

Accepted Answer

Benjamin
Benjamin on 28 Oct 2021
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

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!