Saving the variables in a function call and using them next time the same function is called
이전 댓글 표시
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
채택된 답변
추가 답변 (1개)
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
2013년 11월 7일
Azzi Abdelmalek
2013년 11월 7일
Suppose after calling your function
dx=1;
dy=2;
AF=20
k=1
A(k,:)=[dx dy AF] % store your data in the first row of A
% When you call your function another time and find for example
dx=5;
dy=10;
AF=55;
% increment k and do
k=k+1
A(k,:)=[dx dy AF]
% check your matrix A, it contains 2 rows
d=A(end,:)-A(en-1,:) % will find the difference between the last row and the previous row
and so on
Mahi Nazir
2013년 11월 7일
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
카테고리
도움말 센터 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!