Matlab input string as variable name (or other ways)

Click For Summary
SUMMARY

This discussion addresses the challenge of using input strings as variable names in MATLAB. Users attempted to directly assign user input to a variable for computations like sum and mean, which resulted in errors. The recommended solution involves using the eval() function to convert the input string into a variable, ensuring to check if the variable exists in the workspace with exist(). Additionally, a switch-case structure is suggested for selecting datasets based on user input.

PREREQUISITES
  • Familiarity with MATLAB scripting and functions
  • Understanding of variable scope in MATLAB
  • Knowledge of the eval() and exist() functions
  • Basic understanding of control flow structures like switch-case
NEXT STEPS
  • Explore the use of eval() in MATLAB for dynamic variable assignment
  • Learn about variable existence checks using exist() in MATLAB
  • Investigate control flow structures in MATLAB, particularly switch-case statements
  • Research best practices for user input validation in MATLAB scripts
USEFUL FOR

MATLAB developers, data analysts, and anyone looking to enhance their scripting skills by effectively managing user input and variable assignments.

ghostyc
Messages
25
Reaction score
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
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.
 
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   Reactions: Wrichik Basu and Greg Bernhardt

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
6K