How to input a set of data?? (not by [1 2 3 4 5])

Say the user has a set of data already as a vector call X, and I wish to let the user to input the variable X. Below is my code and the error I got:
D = input('Enter the data set:');
Enter the data set: X
??? Error using ==> input Undefined function or variable 'X'.
What should my code be if I want to let the user to enter just the variable instead of something like [1 2 3 4 5]??? HELP PLEASE!!!

 Accepted Answer

If the input is a string, use
D = input('Enter the data set:','s');
If it's a number use
D = input('Enter the data set:');
But when you enter X, X should be exist

More Answers (1)

Entering a variable name the way you do is fine, but the variable needs to exist in the same scope as the input() statement. If the user defined the variable (such as at the command line) and the input() is within a function then likely the scopes are not the same.
To accommodate this case you could read as a string and evalin('base', D). After, of course, validating that the input is a valid variable name and not (for example) a 'clear all' command.

Categories

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!