1

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?

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.