Reputation: 301
I have lots of variables in the workspace in MATLAB and I have to create some statistics through a function. I need to enter the name of the variable as a function input and I need to return its value in the function and then process it.
varName='Sa'
function [ someStatistics ] = AnalyzeThis (varName);
So, in this AnalyzeThis
function, I need to obtain the value of Sa
(which is a one-dimensional array). How can I do that?
Upvotes: 1
Views: 277
Reputation: 125874
Short answer: use evalin
:
function someStatistics = AnalyzeThis(varName)
varValue = evalin('caller', varName);
% Do stuff with varValue
....
end
Longer answer: you shouldn't really be designing your code to depend on functions like evalin
(such as "evil" eval
). Instead of having a whole bunch of variables with different names in your calling workspace, store the data in structures, which can be easily accessed by field names.
Upvotes: 4