How to create a for loop comparing excel vectors to (sub)folders?

I've been trying to create a loop confirming that each string vector in the excel file (Avanto_Sub2.xls) is equal to the string vector provided in the folder '\test_script_matlab\', named P1,P2,P3,P4 and P5 (foldername). The issue is that I only get "no match" displayed instead of the expected "match" and I don't understand why. When running the script without the loop function I do get the match, so it seems it has to do with the loop. Thanks in advance for your reply.
% code
start_path = fullfile(matlabroot, '\Users\m.laansma\AppData\Roaming\MathWorks\MATLAB\R2012a');
cd '\test_script_matlab\' ;
[~,~,filenames] = xlsread('Avanto_Sub2.xls','A1:A5');
disp(filenames)
ARCHDIR = '\test_script_matlab\';
SUBJIDS = { 'P1' ; 'P2' ; 'P3' ; 'P4' ; 'P5' } ;
disp(SUBJIDS)
for i = 1:length(SUBJIDS)
tst_id = SUBJIDS{i} ;
disp(tst_id) ;
WORKDIR = ARCHDIR ;
if isequal(tst_id,filenames)
disp('match')
else disp('no match') ;
end
end
disp 'finished'

 Accepted Answer

Nothing to do with the use of a loop itself but there's certainly a break in logic in what you have written. For a start, you never use ARCHDIR or WORKDIR so it's unclear what their purpose is. The biggest problem is your isequal line. For isequal to return true, the two arguments must have the same type, size and content, so you've definitively failed the first two conditions and are always going to get 'no match'.
If you want to test that tst_id is present in the cell array filenames then you need to use ismember:
if ismember(tst_id, filenames)
disp('match');
else
disp('no match');
end

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!