File Exchange

image thumbnail

STR2VAR

version 1.0 (1.25 KB) by

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

1 Download

Updated

View License

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].

Comments and Ratings (4)

Thomas

Thomas (view profile)

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.

Jos (10584)

Jos (10584) (view profile)

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

Thomas

Thomas (view profile)

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]);

Jos (10584)

Jos (10584) (view profile)

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.

MATLAB Release
MATLAB 7.2 (R2006a)
Tags Add Tags

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video