I have a function which reads a .mat-file, which contains several vectors. Now I want to analyse just a single of those variables, say variableA
. So what I have in the function is:
variable = variableA;
The rest of the function works on variable
. Consequently, when I want to analyse variableB
, I simply change it in the statement above. Is it possible to pass a string, say 'variableA'
, as an input to the function, so that I can assigns the values of variableA
to variable
?
For example something like this:
string = 'variableA';
variable = something(string); % now variable holds the values of variableA
A construct like the above would make it easier to change between all the variables. All I have been able to find were functions which allow me to assign values to the variable defined by the string and thus not the other way around.
UPDATE
To hopefully clarify my question, say you have the following function:
function analyseFile(name)
%Load the file
load(file.mat)
toAnalyse = %variable from file.mat with name "name" from the argument
%Do some analysis e.g.
time = 0:0.001:(length(toAnalyse)-1)*0.001;
x = lsqcurvefit(@PronySeries,ones(2,1),time,toAnalyse,[0,0],[]);
%And e.g. some plotting
plot(time,toAnalyse)
hold on
plot(time,PronySeries(x,time))
end
Now, I want to specify which variable of the .mat-file should be loaded by the function and subsequently be analysed. Therefore, the argument name
should be used as the variable name.
I am looking for a less error-prone method than eval
. Sorry, this was not noted in the original post. The solution does not have to transform the string to a variable per se. Solutions involving cells would be perfectly fine.