Looping through sets of names for a match and selecting variable with this name
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Zlatomira Ilchovska
am 20 Feb. 2023
Bearbeitet: Stephen23
am 20 Feb. 2023
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?
0 Kommentare
Akzeptierte Antwort
Voss
am 20 Feb. 2023
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)
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
0 Kommentare
Weitere Antworten (1)
Stephen23
am 20 Feb. 2023
Bearbeitet: Stephen23
am 20 Feb. 2023
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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!