MATLAB Answers

0

Matlab function in Simulink - calling variable from workspace

Asked by Herbert Elisadebe on 18 Dec 2017
Latest activity Commented on by Herbert Elisadebe on 19 Dec 2017
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?

  0 Comments

Sign in to comment.

1 Answer

Answer by Jim Riggs
on 19 Dec 2017
 Accepted Answer

Try declaring the variables to be persistent at the top of the function, i.e.
persistent prev_angle prev_counter

  1 Comment

Very helpful suggestion, it actually have solved the problem. Many thanks!

Sign in to comment.