For Loop condition using vlookup function
3 views (last 30 days)
Show older comments
Not sure how to use the for loop with this function. Can anyone help. Thanks. Any help is appreiciated.
The code does not increment through the index(i) or the basenames(i) if they exist. I want them to skip the raw(i) if it doesn't exist and go to the next basename value.
basenames = {'Red' 'Green' 'Orange' 'Purple' 'Pink'};
nbase = length(basenames);
raw = cell(nbase, 1);
for K = 1 : nbase
FileToLoad = [basenames{K} '.xls'];
if exist(FileToLoad, 'file')
[~,~,raw{K}] = xlsread(FileToLoad);
end
end
What I would like to do here is run a for loop that uses the vlookup function from file exchange https://www.mathworks.com/matlabcentral/fileexchange/29233-vlookup-similar-to-ms-excel-function
for i = 1:nbase
[basenames(i),index(i)] = vlookup(raw(i), 'column name', 2, 1);
basenames(i) = cell2mat(basenames(i));
end
What I want to happen is that with the vlookup function I search the .xls file that was previously read into raw (if it exists), then lookup values in column 2 if column 1 label is "column name". Then that puts those values in index. This should loop through only if the raw(i) value exists.
4 Comments
Stephen23
on 8 Jul 2020
Please help us by clicking the paperclip button to upload a sample file.
Based on your previous comment you could try strcmp on the first column and then use the returned logical indices to select the required values from the second column.
Answers (0)
See Also
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!