フィルターのクリア

MATLAB : dynamic variables not updated directly in the workspace

1 回表示 (過去 30 日間)
Sarah Guiffray
Sarah Guiffray 2015 年 3 月 18 日
コメント済み: Adam 2015 年 3 月 18 日
Hello,
I have a problem with some varaibles. I create variables dynamically in a loop.
for i=1:nbr
assignin('base', ['x_',num2str(i)],0)
end
And after, I would like to put the result of my function in these variables. But the variables in the base of the workspace are not updated directly so I have an error "Undefined function or variable". How can I fix my problem ?
for i=1:nbr
['x_',num2str(i))]= fonction(input);
end
Thank you in advance
Best regard

採用された回答

Sarah Guiffray
Sarah Guiffray 2015 年 3 月 18 日
No I really want to call dynamic variable as output because I want to have a matrix for each variables. My function return a matrix with a lot of rows and columns ..
  1 件のコメント
Adam
Adam 2015 年 3 月 18 日
You would still be better off putting those into a cell array. Accessing variables by individual names like that is quite a pain as you are finding out!

サインインしてコメントする。

その他の回答 (1 件)

Adam
Adam 2015 年 3 月 18 日
編集済み: Adam 2015 年 3 月 18 日
Why don't you just create an array instead of a sea of variables?
['x_',num2str(i))]= fonction(input);
is not valid syntax for assigning to a variable. You would have to use eval probably to do that, but if you just create an array instead, e.g.
x = zeros( 1, nbr );
you can simply do
x(i) = fonction(input);
If your results are not scalars then you can use a cell array instead (or if all results are the same 2d/3d size you can still use a standard array with more subscripts for the indexing.

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by