Matlab function in Simulink - calling variable from workspace

조회 수: 3 (최근 30일)
Herbert Elisadebe
Herbert Elisadebe 2017년 12월 18일
댓글: Herbert Elisadebe 2017년 12월 19일
Hi
I want to make a function block/iterative block, which would calculate output value depending on argument and values of some variables from previous iteration. I mean I have, for example current angle, and I subract it from prevoius value of angle. So far I used following code for Matlab Function block:
function angle= calc_angle(counter,dir)
angle = prev_angle + dir*(counter - prev_counter);
prev_angle = angle;
prev_counter = counter;
But variables with prev prefix throws errors, because they are not declared. I tried to just declare it in workspace calling function initializing them, but it haven't helped. How to deal with such calculations? Is there any way to make previous iteration's values visible to function?

채택된 답변

Jim Riggs
Jim Riggs 2017년 12월 19일
Try declaring the variables to be persistent at the top of the function, i.e.
persistent prev_angle prev_counter
  댓글 수: 1
Herbert Elisadebe
Herbert Elisadebe 2017년 12월 19일
Very helpful suggestion, it actually have solved the problem. Many thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품

Community Treasure Hunt

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

Start Hunting!

Translated by