Matlab function into simulink: variable declaration

Hi, I'm programming an arduino due using a matlab function into simulink. As a test, i try to let the on board let blink and count how many times the led has blinked using the matlab function with the folowing code:
function [LED,counter]= blink(pulse)
%#codegen
counter=0;
LED=0;
while true
if pulse == 1
LED=1;
counter=counter+1;
else
LED=0;
end
end
In this code, pulse is a pulse generator, LED goes to the pin of the on board led. With this code, the led doesn't blink because i guess the function doesn't end because of the while loop so i tried another function
function [LED,counter]= blink(pulse)
%#codegen
counter=0;
if pulse == 1
LED=1;
counter=counter+1;
else
LED=0;
end
with this code, the led will blink, but the counter resets after every loop, but without 'counter=0' there is an error because counter isn't initialized. I tried with the model properties, StartFcn to initialize the counter but that doesn't work, anyone who can help me? Thanks!

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 1 月 15 日

0 投票

Use "persistent" ? Though in that particular context I am not sure what the value would initialize to.

2 件のコメント

Thogoe
Thogoe 2016 年 1 月 15 日
Thanks for the answer, but i still get the folowing error
The PERSISTENT declaration cannot be used on function outputs.
Function 'MATLAB Function' (#36.47.57), line 3, column 1:
"persistent"
Launch diagnostic report.
Component: MATLAB Function | Category: Coder error
guess because the persistent command creates an empty matrix, so 'counter' is still undefined.
Walter Roberson
Walter Roberson 2016 年 1 月 15 日
Do not persistent the output variable, persistent the counter variable and copy the counter variable to the output variable.

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

カテゴリ

ヘルプ センター および File ExchangeArduino Hardware についてさらに検索

質問済み:

2016 年 1 月 15 日

コメント済み:

2016 年 1 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by