필터 지우기
필터 지우기

Error when Indexing and object + OOP

조회 수: 1 (최근 30일)
Armindo
Armindo 2016년 1월 27일
댓글: Adam 2016년 1월 28일
Hi,
Can anyone explain me why if I add and objet (PipeLine) to a property (PipeBolt) of another object (Bolt) like:
Bolt(1,1).PipeBolt = PipeLine;
this works well
but if I use:
Bolt(1,1).PipeBolt(N,1) = PipeLine(N,1);
I get an error like: The following error occurred converting from PipeLineObj to double: Conversion to double from PipeLineObj is not possible.
% PipeLine(instance) --> PipeLineObj (class) % Bolt (instance) --> PipeLineBolt (class)
How can I solve this error?

채택된 답변

Adam
Adam 2016년 1월 27일
It likely depends what you are doing before that code. You would be best to pre-size your array of objects with e.g.
Bolt(1,1).PipeBolt = PipeLine.empty(N,0);
to ensure that the property is set to be of the type 'PipeLine', otherwise it will be a double by default, like all variables, so if you didn't explicitly instantiate it and then try to set e.g. its 7th element it will have created it as a double and be expecting a double to be given to it, not some other object type.
In general I initialise all my properties that are to be some other object as e.g.
myProp = SomeClass.empty;
or something like the example above if I expect it to be an array of objects. I cannot remember off the top of my head though - just doing the above and telling it the type of object without presizing may well be enough that your code causing an error would work because it knows the object type beforehand.
  댓글 수: 1
Armindo
Armindo 2016년 1월 27일
Thank you Adam, you are completely right. I do not initialised the object properly.

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

추가 답변 (1개)

Armindo
Armindo 2016년 1월 27일
I solve the other issue but now I have this error:
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
when I do this Bolt(1,1).PipeBolt(2,1)= PipeLineObj.empty
This should not work?
  댓글 수: 1
Adam
Adam 2016년 1월 28일
It should work fine if Bolt(1,1).PipeBolt(2,1) exists and the array is of the correct type.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by