Looping through sets of names for a match and selecting variable with this name

1 回表示 (過去 30 日間)
Zlatomira Ilchovska
Zlatomira Ilchovska 2023 年 2 月 20 日
編集済み: Stephen23 2023 年 2 月 20 日
Hi there! I've got a simple question:
I've got the following code below (ar at least this is a more reproducible version of it; note my .set files are not something I can share on here):
name=dir('*data.set');
name={name.name};
accvectors=[1,1,0,0,1;1,0,0,1,1;1,0,0,1,1]
accvectors_name={'500','501','502','503','504'}
accvectors=cell2table(num2cell(accvectors),'VariableNames',accvectors_name)
for b=1:length(name);
filename = name{b};
[EEG] = pop_loadset(filename);
% match the file to the correct vector of trials
ID = filename(1:3) %just selecting the participant number between 500-599, from a longer file name
for xx = 1:length(accvectors_name);
if xx == ID
part_vector = accvectors{xx}
end
end
end
It is the last 6 lines of code that are a problem. The rest works.
What I want to do is: when the variable name in 'accvectors' matches 'ID', I'd like to select the corresponding 'accvectors' column and save it into 'part_vector'. There is certainly something wrong because instead of a value between 500 and 599 (in my longer dataset), 'xx' just outputs the total count of values (84).
Could someone direct me to where my logic is wrong?

採用された回答

Voss
Voss 2023 年 2 月 20 日
You're comparing a numeric index (xx), which is between 1 and length(accvectors_name), with a length-3 character vector (ID). Instead, compare character vectors to character vectors.
Also, use two subscripts with {}, or use .() with a column name or index to access a table's contents.
accvectors=[1,1,0,0,1;1,0,0,1,1;1,0,0,1,1];
accvectors_name={'500','501','502','503','504'};
accvectors=cell2table(num2cell(accvectors),'VariableNames',accvectors_name)
accvectors = 3×5 table
500 501 502 503 504 ___ ___ ___ ___ ___ 1 1 0 0 1 1 0 0 1 1 1 0 0 1 1
ID = '503';
for xx = 1:length(accvectors_name)
if strcmp(accvectors_name{xx}, ID)
% part_vector = accvectors{xx} % this is incorrect
% part_vector = accvectors{:,xx} % these four all do the same thing
% part_vector = accvectors.(xx)
% part_vector = accvectors.(accvectors_name{xx})
part_vector = accvectors.(ID)
end
end
part_vector = 3×1
0 1 1

その他の回答 (1 件)

Stephen23
Stephen23 2023 年 2 月 20 日
編集済み: Stephen23 2023 年 2 月 20 日
replace
if xx == ID
with
if strcmp(ID,accvectors_name{xx})
"There is certainly something wrong because instead of a value between 500 and 599 (in my longer dataset), 'xx' just outputs the total count of values (84). "
This is how you defined xx:
for xx = 1:length(accvectors_name);
And so that is exactly what xx is: the integers from 1 to ... 84, apparently. After defining xx to have exactly those values, how do you expect xx to have some other completely different values?
A simpler approach using STRNCMP and logical indexing and no table:
V = [1,1,0,0,1;1,0,0,1,1;1,0,0,1,1];
C = {'500','501','502','503','504'};
S = dir('*data.set');
for k = 1:numel(S)
F = S(k).name;
X = strncmp(F,C,3);
M = V(:,X)
end

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by