0

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.

Nicky Mattsson
  • 3,052
  • 12
  • 28
  • 2
    Please don't actually do this. Use either a `cell` or `struct` with dynamic field names to store your data. – Suever Jul 11 '16 at 12:26
  • It would be good if you can enlighten us for what purpose this is used. I'm convinced there is a better solution for your problem, which does not use `eval`. You say that you are reading the data from a file... Probably you could store the contents in a struct and then accessing the fields with your string... It would be beneficial to add some more details to the question, so we can give you a solution to your *real* problem. – Matt Jul 11 '16 at 12:32
  • I have updated the question with further information, I hope this clarifies. – Nicky Mattsson Jul 11 '16 at 13:34
  • Please don't write separate updates to your post. Rather, make it all one seamless thing. If people are interested in how it's changed over time, they can always look at the revision history; it doesn't have to be noted in the raw text – Nic Jul 11 '16 at 19:50
  • As your question is worded now, it sounds like you want to "convert a string to a variable name". You could edit your question and the title to make it more like "*How can I use a string to load a variable stored in a .mat-file?*". This would be answerable and is the *actual* question coming from the update, which has definitely an answer without using `eval`. – Matt Jul 11 '16 at 20:08

0 Answers0