Royeh
Royeh

Reputation: 443

Matlab: modifying one field of defined structure in other used function

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?

Upvotes: 0

Views: 75

Answers (1)

Poelie
Poelie

Reputation: 713

Variables modifed within a function do not change outside of the function. You should return data as the output of function ul. For example:

function data=ul(data,m)
data.Aeq(m,m) = 1
end

n = 4;
data = struct();
data.Aeq = zeros(n);
data.beq = zeros(n,1);
m =3;

data = ul(data,m)

data.Aeq should now be modified correctly.

Upvotes: 3

Related Questions