Mainland
Mainland

Reputation: 4554

How to save a value generated in middle of a MATLAB simulink operation

I have MATLAB Simulink model running successfully. In this model, a function runs code to do the operation. The function code generates a value during the operation. I want to save this value and use at successive operations. enter image description here

In above screen shot, Icur_in and Icur_ou are input and output of the function. In fact both refer to same value. I am using memory function to hold the value for next operation. Up to this point is fine. But the value keeps on changing.

My present code is:

Function [Icur_ou] =  fun(Icur_in)

Icur_ou = Icur_in;
if somecondition
   Icur_ou = I_s; 
end

end

Upvotes: 0

Views: 647

Answers (1)

avermaet
avermaet

Reputation: 1593

I am not 100% sure about what your problem is, since you state: "Up to this point is fine." And also your model looks fine to me, but nevertheless I found this link, which seems to be about the same problem. Besides the suggested solutions, I also liked the first two comments about persistent variables, which I think could maybe also help you depending on the sample time of your model (see comments in the link).

In this case, you would not need the memory block, but instead you'd have to make your Icur_ variable persistent, so that it stays in memory between function calls. Similar to this:

Function [Icur_ou] =  fun(Icur_in)

persistent Icur_ou = Icur_in;
if somecondition
   Icur_ou = I_s; 
end

end

Upvotes: 1

Related Questions