File Exchange

image thumbnail

Produce list of all cases in the current 'switch' block

version 1.4 (11.4 KB) by Rody Oldenhuis
Generate a list of all cases handled by the current 'switch' control structure

1 Download

Updated 02 May 2020

GitHub view license on GitHub

GETCASES Return all cases handled by a switch structure
When called inside a 'switch', C = GETCASES() will return a cellstring C which contains all cases handled by that switch. This can be very useful when constructing error messages, for example:
switch value
case 1
case 2

case 3

C = getCases; % == {'1' '2' '3'}
error(['Invalid option: ''%d''. ',...
'Valid options are: ' C{:}], value);

Normally, the list of all cases handled by the switch needs to be maintained in two different places -- individual items next to all 'case' keywords, and a list of all items at the error message.

When new cases are added, old ones are removed or changed, it is all too easy to forget that the error message(s) need to be updated as well. Especially for large switch structures that handle many cases and have many actions per case, this often leads to situations where the error message(s) list a different set of cases than are actually handled.

GETCASES() automates this process by taking away the need to keep two separate, yet identical lists. It simply traverses the current 'switch' and identifies all different cases it encounters, collecting them in a cell string.


C = GETCASES() will return a cell string C containing all the cases handled by the corresponding switch, as they are written in the code.

C = GETCASES('eval') will return a cell string C containing all the cases handled by the corresponding switch, as they are seen by the 'case' keyword. In other words, they are passed through 'evalin' before adding them to the cellstring.

GETCASES('error') will issue a standard error, listing the given value at the switch, and all the valid cases as shown in the example above. The cases will be listed as they are written in the code.

GETCASES('eval', 'error') or GETCASES('error', 'eval') will do the same, except with the interpreted expressions (see 'eval' above). In both these use cases, there is no return argument.

GETCASES() may be called either from an ''otherwise'' block, or from a ''case'' field. In both cases, the complete list of cases is returned. GETCASES() will return an error message when it is called outside a
switch structure.

GETCASES() interprets the M-code its called from. That implies it cannot be used in MATLAB coder/Embedded MATLAB programs. It can also not be used when called from a switch defined on the MATLAB command line, or equivalently, called from code executed with 'Execute selection' (F9) or from cell mode (Ctrl+Enter).

Cite As

Rody Oldenhuis (2020). Produce list of all cases in the current 'switch' block (, GitHub. Retrieved .

Comments and Ratings (10)

Dan Thomsen


This function offers a convenient method to build in some error handling to your functions.
Thanks for this submission Rody!

Adam Danz

@Adam: fixed :)

Adam Danz

That would be great, Rody. I don't think there's anything else out there that lists cases within a switch block. Thanks!

@Adam That's because of this:
I used an undocumented feature, and they removed that in R2015b :(
I'll post an update shortly.

Adam Danz

Anyone else getting this error in matlab vs 2016b?

Error using getCases (line 149)
Could not read file.
Caused by:
Error using builtin
Cannot find builtin function '_brace'

@Charles Thanks for pointing that out!

The hard thing about this bit is that according to the MATLAB M-language specification, anything written after the line-continuation ellipsis is treated as a comment.

Therefore, your fix will work, but only for your particular case. However, it would brake cases like this:

str = ['this is a long string ',... NOTE: bug#45
'continued on multiple lines'];

which is valid MATLAB.

I see this far more often than your case, so I'm going to leave it as it is.

Thanks again for the feedback, it is really much appreciated.


Thanks, this is really quite helpful!

I did find a bug though in the file. I think you should replace (around line 195):

continued = regexp(code(:,1), '\.\.\..*');
continued = regexp(code(:,1), '\.\.\.$');

I had problems because I was displaying a message with ellipsis using disp('something something ...') on the line just before calling getCases, and the function assumed that it was code distributed on 2 lines and tried to re-construct it on one line, loosing the getCases. I am no expert in regexp, so there might be a better way to correct that, just wanted to share because I couldn't understand why it wasn't working all of a sudden!


Works great out of the box. The cases have an extra set of single quotes that caused a few hiccups in my case, but it was easy enough to work around by adding

for c=1:length(cases)
end %

between lines 345 and 346. I'm sure there are smarter ways to do this, but I was in a hurry.



See release notes for this release on GitHub:

(no code changes; just updated the PayPal link)

[linked to Github]

Updated contact info

1) Added reference to the originator of the idea
2) Added another limitation in the documentation

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux