Matlab input string as variable name (or other ways)

In summary, the conversation is about a user asking for help with using a variable as input in a Matlab function. They have tried using the input() function and also considered adding an argument in their function, but neither method has worked. Another user suggests using eval() to execute the input string as code, but also cautions about checking that the input is a valid variable name.
  • #1
ghostyc
26
0
Hi,

I am writing a Matlab function(script), which take some input.

I have several datasets in the workspace, say dataset1,dataset2,dataset3, ect

In my function, I have tried to use

data = input('Please input your dataset', 's')

and then use 'data' as a variable for some computation, say

sum(data);
mean(data);

It does not work. I wonder what's the 'correct' way to do it?

=====================================================

OR, I can add an argument in myfunction, say

function y = myf(ds,para)

use 'ds' to indicate the dataset that i want to use.

However, in this case, how should i use 'fminsearch' for optimization?
'fminsearch' will then treat this 'ds' input as a parameter.

=====================================================

Thanks!
 
Physics news on Phys.org
  • #2
No, that is not possible. You cannot take the variable name as input. Use either switch-case or if-else:
Code:
choice = input('Enter dataset number: ');
syms data;
switch choice
    case 1
        data = dataset1;
    case 2
        data = dataset2;
    %so on...
end
Then use "data" as your variable.
 
  • #3
ghostyc said:
Hi,

I am writing a Matlab function(script), which take some input.

I have several datasets in the workspace, say dataset1,dataset2,dataset3, ect

In my function, I have tried to use

data = input('Please input your dataset', 's')

and then use 'data' as a variable for some computation, say

sum(data);
mean(data);

It does not work. I wonder what's the 'correct' way to do it?

The original post was from 7 years ago, but for the benefit of anyone else:

eval() takes a string argument and executes it as code.

It is of course necessary to check that the input string is the name of a variable before executing it, as the example on that page shows (but without telling the user that they've done something wrong).

Code:
datasetName = input('Please input your dataset', 's')

if(exist(datasetName, "var"))

   data = eval(datasetName)

else

   error("There is no variable called %s", datasetName)

end
 
  • Like
Likes Wrichik Basu and Greg Bernhardt

1. Can I use user input as a variable name in Matlab?

Yes, you can use user input as a variable name in Matlab. You can do this by using the eval function, which evaluates a string as a Matlab expression.

2. How do I convert a string into a variable name in Matlab?

To convert a string into a variable name in Matlab, you can use the genvarname function. This function checks if the string is a valid variable name and if it is not, it adds an underscore and a number to make it a valid variable name.

3. What is the difference between using eval and genvarname for input string as variable name in Matlab?

The main difference between using eval and genvarname is that eval evaluates a string as a Matlab expression, while genvarname converts a string into a valid variable name. So, if you want to use user input as a variable name, you should use eval. If you want to convert a string into a valid variable name, you should use genvarname.

4. Can I use characters other than letters and numbers in a variable name in Matlab?

Yes, you can use characters other than letters and numbers in a variable name in Matlab, as long as the variable name starts with a letter and follows the same rules as naming any other variable. However, it is recommended to use only letters, numbers, and underscores to avoid any potential errors.

5. Is it possible to use user input as a variable name without using eval or genvarname in Matlab?

No, it is not possible to use user input as a variable name without using eval or genvarname in Matlab. These functions are specifically designed for converting strings into variable names and without them, user input will be treated as a string and not a variable name.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
Replies
1
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
Back
Top