Function cannot read more than 3 objects as input ?
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello, I have a function with four objects as inputs :
function test(obj.T1, obj.T2, obj.T3, obj.T4)
But when I run the fucntion the obj.T4 is not read at all and thus makes the function fail (I need some variables located in the T4 object).
I tried to change the order of the inputs like this :
function test(obj.T1, obj.T4, obj.T3, obj.T2)
And this time this is the T2 object which is not read !
Am I missing something ? This code used to run smoothly but I don't know if it was pure luck or if this structure should work in any cases...
댓글 수: 6
dpb
2021년 11월 16일
편집: dpb
2021년 11월 16일
function test(obj.T1, obj.T2, obj.T3, obj.T4)
Don't use structure components as dummy arguments; either receive the object/struct itself and reference the fields it is by contract with the caller required to contain or use variables of the proper type and with meaningful local names internal to the function.
...
Var4 = T2.Pp.Pr*25;
Var5 = T2.Ca.Vp.Td;
...
tab = ones(1,T4.objA1.G0);
The structures T2 and T4 are undefined in the function before trying to use them.
That they're defined/extant in the caller workspace is irrelevant inside the function; every function has its own private workspace. If you need T2 and T4 inside your function, then pass them in the argument list (and use the struct root as the variable).
By the code posted, something like
function test(T2, T4)
...
would be one reasonable refactorization of the function.
Alternatively, (while I strongly doubt I would choose to do it that way, if you really are going to need all four of these structs at some point in the function and it's just not yet complete, if you were going to package them, then
function test(obj)
T2=obj.T2;
T4=obj.T4;
...
creates local copies that will go away automagically when the function exits.
Of course, one could use the more verbose form of obj.T2.xxx throughout and save the local memory/copy.
채택된 답변
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!