For Loop condition using vlookup function

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 个评论

The code does index through all of the basenames .
The code does skip the raw if the basename does not exist; the corresponding entry in raw will be left empty.
Are you asking to read the basenames from an xls file? Are you asking that once a basename file has been found and the raw for it has been loaded, that something should be done with the raw data present there, such as... cross-checking the basenames stored in the raw with the existing basenames? Adding new basenames to the list if they do not already exist??
I do not understand what you are trying to do.
"What I would like to do here is run a for loop that uses the vlookup function from file exchange"
Why do you need to use a third-party function instead of basic indexing?
The first part of the code does index through all of the basenames and the code does skip the raw if the basemanes does not exist as you have mentioned.
I would like to run a loop that uses indexing similar to the vlookup function to 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.
Maybe there is an easier way to do this using basic indexing?
for i = 1:nbase
[basenames(i),index(i)] = vlookup(raw(i), 'column name', 2, 1);
basenames(i) = cell2mat(basenames(i));
end
I'm only using the referenced vlookup function from file exchange because I'm having a hard time with the basic indexing.
Any help is greatly appreciated.
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.

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

提问:

2020-7-6

评论:

2020-7-8

Community Treasure Hunt

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

Start Hunting!

Translated by