Reputation: 257
There are few variables in my matlab workspace, lets say a and b. eg: a = 1:5; b = 1:10;
I used who
to get their names.
like listVariables = who;
now listVariables
has the variable names a
and b
, but i dont know how to access their values so that I can do some mathematical operations on them.
Upvotes: 1
Views: 87
Reputation: 32084
It looks like evalin is what your are searching for:
a_val = evalin('base', listVariables{1});
b_val = evalin('base', listVariables{2});
The advantage of evalin
is that it can be executed from a function (out of the scope of the workspace).
Example:
In workspace:
a = 1:5; b = 1:10;
Content of my_fun.m
:
function my_fun()
listVariables = evalin('base', 'who');
a_val = evalin('base', listVariables{1});
b_val = evalin('base', listVariables{2});
display(a_val);
display(b_val);
Result of my_fun()
execution:
a_val =
1 2 3 4 5
b_val =
1 2 3 4 5 6 7 8 9 10
Note: there are cases in which evalin
is useful, but it's not a good coding practice.
Upvotes: 1