フィルターのクリア

modifying one field of defined structure in other used function

2 ビュー (過去 30 日間)
Saber
Saber 2015 年 3 月 12 日
コメント済み: Saber 2015 年 3 月 12 日
I have defined a data structure `data` with 7 fields. Two of the fields is as:
n = 4;
data = struct();
data.Aeq = zeros(n);
data.beq = zeros(n,1);
m =3;
Now, there is another function `ul(data,m)` that I am passing the `data` and `m` as inputs. Inside `ul()`, I will modify one component of matrix `Aeq` as:
data.Aeq(m,m) = 1;
after running whole the code when I am checking `data.Aeq` it is still zero matrix while I have modified on component. Am I doing something wrong?
  1 件のコメント
Brendan Hamm
Brendan Hamm 2015 年 3 月 12 日
Please post the function which is supposed to modify the data.
Likely you are trying to use a pass by reference functionality, while MATLAB is pass by value.

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

採用された回答

Guillaume
Guillaume 2015 年 3 月 12 日
As Brendan has stated in his comment, matlab is exclusively pass by value. A function always receive a copy of the input arguments, so modifications are only local to the function.
To solve your problem, modify your function declaration to:
function data = ul(data, m)
%...body of the function
data.Aeq(m, m) = 1; %modify local copy which is returned by the function
%...more code
end
and call your function with:
data = ul(data, m);

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by