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.

 채택된 답변

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

릴리스

R13SP1

Community Treasure Hunt

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

Start Hunting!

Translated by