How can I do dynamic calculation?

How can I update the following variable?
a = 1; b = a + 2;
then I changed a = 2, but b is still 3.
How can I change or update b without executing b = a + 2?
If Matlab supports pointer in C, I guess the above problem is solved easily.

댓글 수: 1

John D'Errico
John D'Errico 2017년 6월 13일
But you are using MATLAB, not C. No pointers available.

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

답변 (2개)

Matt J
Matt J 2017년 6월 13일
편집: Matt J 2017년 6월 13일

0 개 추천

Even in C, I think a re-calculation would have to occur somewhere in the code to get a revised value. However, the kind of auto-updating I believe you're looking for is something a dependent class property can do:
classdef myclass
properties
a
end
properties (Dependent)
b
end
methods
function val=get.b(obj)
val=obj.a+2;
end
end
end
Now you can do things like this,
>> obj=myclass;
>> obj.a=1; obj.b
ans =
3
>> obj.a=3; obj.b
ans =
5
Walter Roberson
Walter Roberson 2017년 6월 13일

0 개 추천

syms a
b = a + 2
a = 1
subs(b)
a = 2
subs(b)

카테고리

도움말 센터File Exchange에서 숫자 및 정밀도에 대해 자세히 알아보기

질문:

2017년 6월 13일

답변:

2017년 6월 13일

Community Treasure Hunt

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

Start Hunting!