MATLAB: Interchange variable names and strings

MATLAB: Interchange variable names and strings

stringsvariable names

How do I convert a string to a variable name (and vice versa)?
I want to read in a string, and use it as a variable name, such as when I load a *.mat, in which the variable names depend upon which program generated/saved the *.mat.
I can see it useful to convert a variable name to a string.

Best Answer

  • Consider using
    S = load('YourMATFile.mat');
    the_var_names = fieldnames(S);
    second_var = S.(the_var_names{2});
    You can slice, dice, trice, even julienne!
  • Add a Comment

    Your email address will not be published. Required fields are marked *