From string to matrix name
古いコメントを表示
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 件のコメント
@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,...
採用された回答
その他の回答 (2 件)
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
カテゴリ
ヘルプ センター および File Exchange で Whos についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!