How to set a variable name by user input in MATLAB

In summary, the user is to enter the name of the variable to be used in the program and the program will assign the value 10 to it.
  • #1
Weroc
4
0
Think there is a simple solution to this but couldn't find any... Well as the title describes, i am wrote a program that generates, sorts, does some calculations and assings all results to a variable which is set before. Can i get the user assign the variable name which results will be assigned? I've been searching for more than 3 hours now but couldn't find any solution including MATLAB help, Doc files, Forums... I know it's not critically needed and program runs without problem ATM but let's say i like to spice things up :)

For further explanations,

Lets say my results are in x=1 2 3 ... n
I want the user to assign the name which results will be in... like userinput=1 2 3 ... n

Thanks for any help..
 
Physics news on Phys.org
  • #2
Have you looked at the eval() function?
 
  • #3
Yes i did, but i am a starter and couldn't find the usage of eval or evalin for this situation the samples i saw in the doc and forums were a bit too complex for me to understand... I would really appreciate a decent usage pattern of eval or the exact code i need to make that happen :) either would be great.
 
Last edited:
  • #4
Ok, i am trying to use eval for this thing but get an error message,

i just typed

s=input('test','s')
eval(s)

but MATLAB tells me that the values i enter for input are not defined before but, how can i define some variable before it's been created as i don't know which variable user will pick?

? Error using ==> eval
Undefined function or variable 'k'.

Error in ==> Srala at 5
eval(s);

the part i am having problem is like below..

Code:
disp('NOT: Bu program rastgele belirlenen "n" kadar sayıyı sıralar ve bu değerlerli "s" değişkenine vektor olarak atar.')
n=input('Kaç adet veri sıralanacağını yazın = ');
s=input('test','s');
eval(s);
t=rand(1,n);
for x=1:n;
    s(x)=min(t);
    ind=find(t==min(t));
    t(ind)=[];    
end
disp ([num2str(n), ' Adet rastgele sayının sıralı hali: ', num2str(s)])

Also, i know there is a "Sort" command that does the same thing... but this part is used to program the sort function manually... It's like a challenge term :)
 
Last edited:
  • #5
This asks the user for the name of the variable and sets its value to 10:

value = 10;
variableName = input('Enter variable name: ', 's');
eval([variableName '=' num2str(value)])


It would be wise to check variableName first with isvarname to make sure it's a valid variable name.
 
  • #6
thanks a lot for your answer, i can use the code you write to create a varname and assign values to it but the varname i want to let user set is seen as s(x) in the program... can i write some syntax to keep that place for the user assinged varname?
 

1. How do I prompt the user to input a variable name in MATLAB?

To prompt the user for input in MATLAB, you can use the input function followed by the variable name in single quotes. For example: my_variable = input('Enter a variable name: '). This will display a prompt for the user to enter a variable name and store the input in the variable my_variable.

2. Can I set a variable name using characters or spaces instead of numbers?

Yes, you can use characters or spaces to set a variable name in MATLAB. However, the variable name must follow certain rules. It must start with a letter, followed by any combination of letters, numbers, or underscores. Spaces are not allowed in variable names, but underscores can be used to separate words.

3. How do I check if a variable name is already in use in MATLAB?

To check if a variable name is already in use in MATLAB, you can use the exist function followed by the variable name in single quotes. If the variable name exists, the function will return a value of 1. If the variable name does not exist, it will return a value of 0.

4. Can I use user input to create multiple variables in MATLAB?

Yes, you can use user input to create multiple variables in MATLAB by using a loop. For example, you can prompt the user to enter the number of variables they want to create, and then use a for loop to create and name each variable based on the user's input.

5. Is it possible to change a variable name after it has been set in MATLAB?

Yes, you can change a variable name after it has been set in MATLAB by using the eval function. This function allows you to evaluate a string as a MATLAB expression. So, you can prompt the user for a new variable name, store it in a string, and then use eval to change the variable name. However, it is not recommended to use this method frequently as it can make your code more difficult to read and debug.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
231
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
995
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
823
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top