How can I pass a variable to a function with the variable name and data visible to the function without passing both a string and a data variable separately in MATLAB 7.0 (R14)?

I want to pass a variable to a MATLAB function with the variable name and data visible to the function without passing both a string and a data variable separately in MATLAB.

 Accepted Answer

You can accomplish this by using the EVALIN function. As an example, if you want to write a function to plot a workspace variable and set the plot title to the name of the variable, you could use the following function:
function plotname(name)
% where name is passed as a string
data = evalin('base',name);
plot(data)
title(name)
an example of using this code from the MATLAB command prompt is below:
myData = 1:10;
plotname myData %can also use plotname('myData')
This will plot the data contained in "myData" and set the title of the plot to be "myData".

More Answers (0)

Products

Release

R13SP1

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!