There is a major bug in Matlab in which the field of a class is modified when passed as an function argument but not requested as an output if the calling function modifies the value internally. In the minimal example below: b.value is not the same before and after calling test.
b.value = 10;
b.value,
a = test(b);
b.value
function a = test(b)
a=1; b=b.value*10;
end

댓글 수: 3

Walter Roberson
Walter Roberson 2018년 6월 1일
I am not seeing anything when I test your code in R2018a.
If b was being modified by the code you post, then it would no longer be a struct (because you are writing over all of b), and b.value would then be a failure.
Your question talks about "field of a class", but your demonstration involves a struct rather than a class.
Antonio Mei
Antonio Mei 2018년 6월 1일
Right, it only happens for when b is defined via classdef.
Walter Roberson
Walter Roberson 2018년 6월 1일
Please post actual example including classdef

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

 채택된 답변

Steven Lord
Steven Lord 2018년 6월 1일

2 개 추천

I tried this with a very simple class and it worked as I expected.
It sounds to me like b is an instance of a handle class. If that's the case, the behavior you described is correct as described in the "Handle Objects Modified in Functions" section on that documentation page (assuming that in your test function you assigned into the value property of the b object rather than overwriting the variable inside the function.) See the attached example. I expect the last line printed when you run that code to be "ans = 100".

댓글 수: 1

Antonio Mei
Antonio Mei 2018년 6월 1일
Thank you for clarifying. I actually had used the "dynamicprops" property. Do you know of a way to dynamically define properties without recursing to a handle subclass?

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

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

제품

릴리스

R2017b

질문:

2018년 6월 1일

댓글:

2018년 6월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by