File Exchange

image thumbnail

vlookup - similar to MS Excel function

version 1.0.0.0 (1.57 KB) by Zhiqiang Zhang
A matrix version of VLOOKUP similar to MS Excel function.

5 Downloads

Updated 02 Nov 2010

View License

VLOOKUP the function as vlookup in Excel

[content, index] = vlookup(m, e, column, lookcolumn) look for 'e' in the 'lookcolumn'-th column of 'm', and return the coresponding 'column'-th element of 'm' in the same row.

the 'm' could be a numeric matrix of a cell matrix.

lookcolumn is 1 by default if omitted.

Example:

m = {1, 'a', [2 3];
2, 'b', 'cd'
3, 'a', true;};
[content, index] = vlookup(m, 'a', 3, 2) then
content = {[2 3], 1};
index = [1;3]

author: http://zhiqiang.org/blog/tag/matlab

Cite As

Zhiqiang Zhang (2021). vlookup - similar to MS Excel function (https://www.mathworks.com/matlabcentral/fileexchange/29233-vlookup-similar-to-ms-excel-function), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (11)

Stephen Cobeldick

1. MATLAB is not MS Excel.

2. Learning any new programming language means learning the features of that language.

3. MATLAB's indexing is simple, powerful, efficient, and much more versatile than Excel's VLOOKUP.

4. Hiding basic indexing inside a third-party function delays when beginners will actually have to learn how to use indexing.

Two stars for working as documented. Two stars for obfuscating basic indexing inside a third-party function and delaying when beginners will actually need to learn how to use indexing by themselves.

Stephen Cobeldick

Ugh, so this is how beginners avoid learning how to use MATLAB properly.

Indexing is one of MATLAB's most basic, powerful, and useful features. Not using indexing is like driving a car stuck in reverse: sure it works, but it is slow and does not take advantage of the car's abilities... and to be honest, imagine how people would look at the driver as they drive slowly past, focusing carefully on the rear-view mirror...

This submission obfuscates indexing behind a pointless third-party function.

Learning how to use indexing will be much more beneficial than this function can ever be.

James

Does anyone know how to use this function for looking up 2 columns and then get the value from the corresponding third row/column?

Roy Axford

As a longtime Excel user, I use vlookup frequently. I'm surprised that MATLAB doesn't have an equivalent built-in function. Therefore, I am delighted with this function. Thank you Zhiqiang Zhang!

Alfonso Garrido

Hey, how can I extract the vector which is the first element of content. Im a bit new to matlab. Thanks

Kalvin Low

Y He

Victor Lejona

Sergiy

Darin McCoy

BTW - you might want to consider making a change on line 33 of this from

index = find(strcmp(e, m(:, lookcolumn)));

to

index = find(strcmpi(e, m(:, lookcolumn)));

The second one finds things if they are not case sensitive

Darin McCoy

This is really helpful!

MATLAB Release Compatibility
Created with R2010b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

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

Start Hunting!