0

Assume the variable FileName contains a string such as Name1. How do I make a variable with the name Name1?

The example 4 at this page seems to be similar, but I cant get it to work. Is it the right way to do it? http://se.mathworks.com/help/matlab/ref/genvarname.html

Student tea
  • 127
  • 6

2 Answers2

3

What you see in "Example 4" is accused as bad programming style. The documentation also contains a section why to avoid eval.

I would recommend a struct with dynamic field names to achieve similar.

filename='name1';
mydata=struct();
mydata.(genvarname(filename))=load(filename);

Besides better performance, you also get additional functionality when handling multiple files. For example structfun to apply a function to all your data or fieldnames to get all filenames.

Daniel
  • 36,610
  • 3
  • 36
  • 69
  • 2
    As a side note ... string `filename` must be a valid property name (no space, no `&`, start with a letter, must not be a keyword and must be less than [`namelengthmax`](http://fr.mathworks.com/help/matlab/ref/namelengthmax.html) ) ... Name validity can be tested with [`isvarname`](http://fr.mathworks.com/help/matlab/ref/isvarname.html) – CitizenInsane Feb 27 '15 at 16:38
  • 1
    Plus 1 for avoiding `eval`. I would have done the `struct` approach myself. – rayryeng Feb 27 '15 at 16:39
  • Side note also, `mex` function [`mxCreateStructArray`](http://fr.mathworks.com/help/matlab/apiref/mxcreatestructarray.html?searchHighlight=mxcreatestructarray) does not test its parameters and allows creating structure with invalid field names (see this [mex source](https://gist.github.com/CitizenInsane/88376e034571550a3b82))... Came across the situation no more than 3 days ago and reported this small issue to TMW. – CitizenInsane Feb 27 '15 at 16:47
  • 1
    @CitizenInsane: Added `genvarname` to the solution. Converts to valid variable names, which should also be a valid field name. – Daniel Feb 27 '15 at 16:49
  • That code produces error message:Error using load Unable to read file 'name1': no such file or directory. – Student tea Feb 27 '15 at 17:03
  • @Studenttea: The code is an example supposed to load `name1`, you obviously have to insert your file name. – Daniel Feb 27 '15 at 17:26
1

For what you want to do, the eval function is there for you:

FileName = 'Name1';
eval([FileName ' = 18;']);      % Executes "Name1 = 18;"

and now the variable Name1 is created and has a value of 18.

The function genvarname has a different purpose, which is to generate acceptable and non-conflicting variable names, and not the variables themselves.

Ratbert
  • 5,463
  • 2
  • 18
  • 37
  • How do you access the variable later in your function? For instance, if you wanted to increment `Name1`? You can't just do `Name1 = Name1 + 1;` because `Name1` isn't know beforehand, right? – beaker Feb 27 '15 at 17:36
  • No, it would work since eval actually creates a Name1 variable in the workspace – Ratbert Feb 27 '15 at 17:39
  • How did you know to call the variable `Name1` later in the function but not when you created it? – beaker Feb 27 '15 at 17:42