From string to matrix name

조회 수: 29 (최근 30일)
Zuza Swirad
Zuza Swirad 2016년 4월 28일
편집: Stephen23 2016년 4월 29일
Hi, I would like to enter a specific value in a martix whose name is created by string. I've got a set of input tables 'input1', 'input2' ... 'inputn' and want to fill the same cell in each of them with a specific value.
I unsuccessfully try:
for n=1:12
name=sprintf('input%d',n);
name(2,2)=3;
end
Any suggestions? Thanks
  댓글 수: 1
Stephen23
Stephen23 2016년 4월 29일
편집: Stephen23 2016년 4월 29일
@Zuza Swirad: What you are doing is a really slow and buggy way to write code. For some reasons many beginners think that dynamically accessing variables is a great idea: But really it isn't. There have been lots of discussion on this topic, if you really want to solve your question in a robust way then you need to avoid accessing variables names dynamically.
When you write code properly (without dynamic variable names) then it is faster, neater, more robust, easier to debug, less obfuscated, much more efficient, etc., etc.
Read this to know more about why your "solution" with eval is terrible way to write code:
Even worse, using eval removes all of the inbuilt code checking tools that MATLAB has, which check your code as you write it in the editor One would think beginners would find these code checking tools useful, but instead they decide to use slow, buggy code that turns off all code helper tools...
Keep your data easily accessible in one variable. it is much faster, easier to access, easier to debug,...

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

채택된 답변

Star Strider
Star Strider 2016년 4월 28일
I cannot test your code, but you probably have to use the eval function to do what you want:
name{n} = eval(sprintf('input%d',n));
You also need to subscript your name variable somehow. I used a cell array here because I don’t know what your ‘input1’ and other variables contain, and a cell array will work. See the documentation on Cell Arrays for details on how to work with them if you have not used them before. (Change it to a numeric array if that works best for you.)
The eval function is normally to be avoided, but at least you are using it to do the correct approach, that being to take several different variables and combine them in a single matrix.
NOTE Note that this is UNTESTED CODE but it should work. You may need to experiment with it, though.
  댓글 수: 1
Stephen23
Stephen23 2016년 4월 29일
편집: Stephen23 2016년 4월 29일
@Star Strider: I think you misread the question: "I've got a set of input tables 'input1', 'input2' ... 'inputn' and want to fill the same cell in each of them with a specific value"
It does not mention merging the data into one variable, it describes adding some value to each of the existing variables.
The separate variables still exist, and presumably will get accessed again by some slow and buggy method.

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

추가 답변 (2개)

Renato Agurto
Renato Agurto 2016년 4월 28일
str = [name '(2,2) = 3'];
eval(str);
you could also try to define the tables as a cell array to avoid using eval
for n=1:12
input{n}(2,2)=3;
end

Zuza Swirad
Zuza Swirad 2016년 4월 29일
Hi both,
thanks a lot for your replies! I managed to do it by using sprintf, regexprep and eval.
I failed every time I tried to access the matrix by input{n}(2,2) though. (it would be the fastest)
Thanks!

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by