Saving the variables in a function call and using them next time the same function is called

9 次查看(过去 30 天)
I call a function few times within a loop. Each time the function is called it calculates the value of dx,dy and AF. The next time the same function is called I need to compare the dx and dy as calculated in previous call with the ones calculated in this call and if (dx_previous =dx_new and dy_previous =dy_new) then I need to put AF= AF_previous+AF_new.
Can someone please help me with this. How do I do it

采纳的回答

Sean de Wolski
Sean de Wolski 2013-11-7

更多回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2013-11-7
k=1
[dx,dy,AF]=youy_fcb(~)
A(k,:)=[dx dy AF];
k=k+1
[dx,dy,AF]=youy_fcb(~)
A(k,:)=[dx dy AF];
d=A(end,:)-A(en-1,:)
dx=d(1)
dy=d(2)
AF=d(3)
  4 个评论
Mahi Nazir
Mahi Nazir 2013-11-7
I want the function I am calling should be doing all this because it will be plotting at the end.... Like if previous dx and new dx are same and prev dy and new dy are same the AF will be accumulated (Previous_AF + new AF) and the plot3(dx,dy,AF)... This all needs to be done in the function I am calling
Azzi Abdelmalek
Azzi Abdelmalek 2013-11-7
编辑:Azzi Abdelmalek 2013-11-7
function [dx,dy,AF]=your_fcn(~)
persistent old_dx old_dy old_AF
if isempty(old_x)
old_dx=0 % initialize
old_dy=0
old_AF=0
end
% your code
dx=f(old_dx,...)
dy=f(old_dy,...)
AF=f(old_AF,...)
old_dx=dx;
old_dy=dy
old_AF=AF

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Desktop 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by