2.0

2.0 | 1 rating Rate this file 2 Downloads (last 30 days) File Size: 1.56 KB File ID: #10007

parseEnum

by Gerald Dalley

 

15 Feb 2006 (Updated 21 Feb 2006)

Looks up values associated with a given string.

| Watch this File

File Information
Description

val = parseEnum(myStr, str1,val1, str2,val2, ..., strn,valn)

Takes MYSTR and searches for the STRI string that matches it. Returns the corresponding VALI. Returns an empty array if not match is found. Uses STRCMP to do the matching.

Examples:
   parseEnum('foo', 'foo',1, 'bar',2) --> 1
   parseEnum('bar', 'foo',1, 'bar',2) --> 2
   parseEnum('baz', 'foo',1, 'bar',2) --> []

Motiving scenario:

This function was written to clean up and compact code where I wanted to look up a mapping from a string to a value. For example, before I might have had someting like the following (using Matlab 7 FIND enhancements)
   function code = getFruitCode(fruit)
   names = {'apple', 'orange', 'banana'};
   values = [-1 0 1];
   code = values(find(strcmp(names, fruit), 1));
and with PARSEENUM, we can condense it down to
   function code = getFruitCode(fruit)
   code = parseEnum(fruit, 'apple',-1, 'orange',0, 'banana',1);

Changes:

2006-02-18: Uses faster STRCMP implementation as suggested by Duane Hanselman. Removed boilerplate copyright. Returns an empty array instead of generating an error when no match is found. Added a motivating scenario to docs.

MATLAB release MATLAB 7.0.4 (R14SP2)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (6)
17 Feb 2006 Michael Robbins

It should ideally be an object, otherwise it should at least be vectorized. Use reshape and you will not need your loop.

17 Feb 2006 Duane Hanselman

strcmp can compare msStr to varargin(1:2:end) in one call. No loop needed. Why make an error be the only option if no matching string is found? Why not drop the copyright boilerplate? If you need to copyright such simple code, put it on your website and post a link to it on the MATLAB link exchange. Then you can make folks click on an "I agree" to agree to your terms before downloading your code.

18 Feb 2006 Gerald Dalley

I've modified the implementation as suggested by Duane Hanselman to be vectorized. In the docs, I've placed a motivating scenario to show an example of where I find this function useful.

Although I find it more convenient to have an error generated, it is more consistent with Matlab to return an empty matrix when no match is found. I've modified the implementation to do so.

Although plenty of other functions here on Matlab Central have copyright statements, I decided it wasn't worth the hassle and have simply removed the boilerplate.

The updated version should be available by 21 Feb 2006, according to Mathworks.

19 Feb 2006 Michael Robbins

What I meant is that you don't need *any* loops:

A={'foo',1, 'bar',2};
B=reshape(A,length(A)./2,2).';
C=B{strmatch('bar',B(:,1)),2};

20 Feb 2006 Michael Robbins

On second though, you can do it in one line without RESHAPE: A{strmatch('bar',A(1:2:end))*2}

07 Jun 2007 Gerald Dalley

Michael Robbins' suggestions change the behavior for
>> parseEnum('baz', 'foo',1, 'bar',2)
in that they throw errors instead of returning an empty matrix. An empty matrix is more consistent with other Matlab functions.

The looping was removed using an offline suggestion by Duane Hanselman.

Please login to add a comment or rating.
Updates
21 Feb 2006

Removed boilerplate copyright, removed loop as per Duane Hanselman's suggestion, and added a motivating scenario to the docs. Added a post-hoc "inspired by" link to PARSEARGS that tackles a related problem.

Tag Activity for this File
Tag Applied By Date/Time
strings Gerald Dalley 22 Oct 2008 08:15:55
parse enum enumeration switch Gerald Dalley 22 Oct 2008 08:15:55

Contact us at files@mathworks.com