How to initialize a variable only once in an embedded matlab function?

4 visualizzazioni (ultimi 30 giorni)
I'm working on a smart metering system simulation.I have a billing algorithm matlab function inserted in my simulation.The inputs are:
1.No of units of energy consumed
2.Clock
3.Price signal from utility
Outputs are:
1.Remaining balance
2.zero balance indicator switch
I'm trying to use the following algorithm Store the current units consumed in a variable which will become prev units in the next clock cycle.Then i perform current balance-prev balance in next cycle. Multiply the answer with the current price and add it to a variable y0 which will be subtracted from initial balance to get the current balance.
I want prev_bal=0 and y0=0 in the start.But if i write the 2 statements at the start of the code both variables become 0 at the start of every sampling.As a result the algorithm fails.Please guide me how to set the two variables 0 only for the first sampling or suggest an alternate algorithm.
Thanks Nikhil
  1 Commento
MAYANK JHA
MAYANK JHA il 29 Mar 2015
hi Nikhil,
did you find a better solution??... try the "persistent" functionality in Matlab function block to initialize. refer here:http://fr.mathworks.com/help/simulink/ug/defining-and-initializing-persistent-variables.html
use isempty command to check and therefore assign the intial value at first instant.
your welcome. :)

Accedi per commentare.

Risposte (2)

Ryan G
Ryan G il 5 Apr 2013
You can try the exist function. This will check to see if they exist. If they do, then you can skip that line of code.
  2 Commenti
Nikhil
Nikhil il 6 Apr 2013
Modificato: Nikhil il 6 Apr 2013
I get the following error on using the exist function. "Function 'exist' implicitly resolved in the MATLAB workspace. Implicit evaluation in MATLAB is not supported. Please declare this function extrinsic using coder.extrinsic('exist'), or call it using feval. Here is my code after making the function call extrinsic.
function [y0,y1,y2,x1] = fcn(u1,u2)
coder.extrinsic('exist');
a= exist('x1','var');
if a == 0
x1=0;
y0=0;
else
end
y0= y0+(u1-x1)*u2;
x1 = u1;
y1=2000-y0;
if y1 > 0
y2 = 1;
else
y2=0;
end
end
Now i get the following error: Expected either a logical, char, int, fi, single, or double. Just for clarification i'm using an embedded matlab function inside a simulink model.What to do now?
Ryan G
Ryan G il 8 Apr 2013
Will you be generating code for this function? If so, you will need to find another method of doing this.
If not, I think you need to pre-define a before you call exist.
a=0;
a= exist('x1','var');
When you call a function extrinsically it uses the mxarray datatype from MATLAB. MATLAB Function Blocks generate code and it can be a bit confusing as to where you need to pre-define variables if you've never done it before.
The line above essentially declares 'a' as a double type so when you call the exist function it will maintain that datatype.

Accedi per commentare.


harjot
harjot il 29 Ott 2013
Hi Nikhil .. I am facing similar issue . Are you able to initialize the variable inside a function block for just once. I tried the exist function but it seems it dioesn't work inside a function block

Community Treasure Hunt

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

Start Hunting!

Translated by