MATLAB How to set a variable name by user input in MATLAB

AI Thread Summary
The discussion revolves around a user seeking assistance with a MATLAB program that generates and sorts random numbers, wanting to allow users to assign a variable name for the results. The user has attempted to use the eval() function but encountered errors related to undefined variables. They provided a snippet of their code and expressed confusion about how to define a variable before it is created based on user input. Suggestions included using eval to assign values to a user-defined variable name, with an example demonstrating how to check if the variable name is valid using isvarname. The user is looking for a way to incorporate the user-assigned variable name into their sorting logic, specifically in the context of storing sorted results. The conversation emphasizes the challenge of dynamically assigning variable names in MATLAB and the need for clear examples to facilitate understanding.
Weroc
Messages
4
Reaction score
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
Have you looked at the eval() function?
 
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:
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:
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.
 
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?
 

Similar threads

Replies
2
Views
5K
Replies
2
Views
3K
Replies
2
Views
2K
Replies
3
Views
2K
Replies
5
Views
3K
Replies
1
Views
3K
Replies
10
Views
3K
Back
Top