How to set a variable name by user input in MATLAB

Click For Summary

Discussion Overview

The discussion revolves around how to allow user input to define variable names in MATLAB, particularly in the context of a program that generates and sorts data. Participants explore methods to dynamically assign variable names based on user input, while also addressing challenges encountered in implementing these methods.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant seeks a way to let users assign variable names for results generated by their program, expressing difficulty in finding a solution.
  • Another participant suggests using the eval() function to achieve this, prompting further inquiry about its usage.
  • A participant shares their attempt to use eval() but encounters an error related to undefined variables, leading to a request for clarification on how to define variables that have not yet been created.
  • One participant provides a code snippet demonstrating how to assign a value to a variable name specified by the user, suggesting the use of isvarname to validate the variable name.
  • A later reply raises a concern about how to incorporate the user-defined variable name into an existing program structure where the variable is referenced as s(x).

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to dynamically assign variable names, and multiple competing views and methods are presented throughout the discussion.

Contextual Notes

Participants express uncertainty regarding the proper use of eval() and the implications of defining variables dynamically, highlighting potential issues with variable naming conventions and scope.

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
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K