필터 지우기
필터 지우기

Assigning input string as variable name

조회 수: 17 (최근 30일)
David Polcari
David Polcari 2012년 7월 4일
편집: Stephen23 2019년 6월 25일
Hi everyone, I have a quick question. I would like to ask the user to input the name of a variable and then use that string as a new variable name. For example, say I have the value [0 1 2 3 4]. I want to ask the user to give the name he would like to that variable.
So, for example:
x = input('Enter the name of the new variable: ') Say the user enters: new_var
I want:
new_var = [0 1 2 3 4]
Can someone explain how to do this?
Thanks for the help

채택된 답변

Jan
Jan 2012년 7월 4일
This method will be insecure: Imagine the users selects a name, which is already used in your program. Then there is no way to avoid a crash reliably.
Another problem will be evil users, who type:
"system('format C:); y"
Then eval will assign y after it has crashed your computer. I admit, this will not be the standard case. But there are a lot of possible problems, which are less brute.
  댓글 수: 2
David Polcari
David Polcari 2012년 7월 4일
Hi Jan, for my use I highly doubt we could have this problem. The name would basically consist of sample number. For example, current_S1.
Jan
Jan 2012년 7월 5일
@David: Users will create typos, whenever it is possible.
When you will get problems with EVAL-approachs in the future, remember this thread and read the questions of hundrets of other forum users, who suffered from the disgraces of EVAL also. The user-controlled naming of variables is a bad programming practize.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Andrei Bobrov
Andrei Bobrov 2012년 7월 4일
편집: Andrei Bobrov 2012년 7월 4일
x = input('Enter the name of the new variable: ','s');
eval([x,'=0:4;']);
  댓글 수: 2
David Polcari
David Polcari 2012년 7월 4일
Hmm this work but too specific. Let me ask another way. I have a column of values in a text file. I want the user to be able to copy paste the values into matlab and then store them in a variable that they can name themselves. Thanks again!
Jan
Jan 2012년 7월 5일
편집: Jan 2012년 7월 5일
What does "copy&paste the values into Matlab" mean explicitly?

댓글을 달려면 로그인하십시오.


Arash Roozitalab
Arash Roozitalab 2018년 4월 18일

Hello. One might use

assignin('base',var_name, value)

to assign a

value 

to a string

var_name = "given_name"
  댓글 수: 1
Stephen23
Stephen23 2018년 4월 18일
편집: Stephen23 2018년 4월 18일
Note that assignin suffers from all of the same problems as eval does: slow, complex, obfuscated code, buggy, and hard to debug. This does not depend on which command is used, but these problems are caused by the act of magically assigning values to variable names dynamically. See:
Beginners wanting to learn how to write neat, efficient MATLAB code that is easy to debug should focus on keeping their data in as few arrays as possible, and use indexing to access it. As has already been discussed many times on this forum, they should avoid using eval, assignin, etc.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2012년 7월 5일

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by