Code covered by the BSD License  

Highlights from
STR2VAR

Be the first to rate this file! 23 Downloads (last 30 days) File Size: 1.25 KB File ID: #26341

STR2VAR

by

 

Returns the variable or array of variables, VAR, matching the pattern STR.

| Watch this File

File Information
Description

FUNCTION VAR = STR2VAR(STR)

Returns the variable or array of variables, VAR, matching the pattern STR. Matching variables must be of same type.

a = 3;
b = str2var('a'); % b is now equal to a, i.e. 3.

a1 = 3;
a2 = 2;
b = str2var('a*'); % b is now an array with values [3, 2].

MATLAB release MATLAB 7.2 (R2006a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (4)
12 Jan 2010 Thomas

I was just trying to be nice and explain one example (with the creation of an anonymous variable to pass onto a function) for you. It is meant to be a help using the command line, not for "efficient programming". If you do not find any use for it, so be it.

12 Jan 2010 Jos (10584)

One should avoid creating variables like this! Use cell arrays or structs, which allows for both clean and efficient programming.

12 Jan 2010 Thomas

The way I use it, is to avoid having to write long lines in order to create an array, e.g.:

>> whos
S4A1E01 1x1 17297 struct array
S4A1E02 1x1 15893 struct array
S4A1E03 1x1 19313 struct array
... many more ...
S4A3L10 1x1 15825 struct array
S4A3L12 1x1 16081 struct array
S4A3L13 1x1 14513 struct array

Then I write:
>>my_scatter_plot(str2var('S4*'));

instead of:
>>my_scatter_plot([S4A1E01, ...many more..., S4A3L13]);

11 Jan 2010 Jos (10584)

Can you give an example where this might be useful?

Furthermore, it gives rather cryptic error messages to the end-user when the variables cannot be concatenated:
x1.a = 1 ; x2 = {2} ; str2var('x*')
I suggest to catch these errors.

Contact us