How to specify the type of the argument of the function?
조회 수: 2 (최근 30일)
이전 댓글 표시
I have a class of a type BlockInfo containing several attributes. I need to pass an object of the BlockInfo type to the function. I cannot find a suitable syntax for this.
in blockinfo.m file
classdef BlockInfo
%BlockInfo Summary of this class goes here
% Detailed explanation goes here
properties (Access = public)
HueDev double
HueAvg double
HueMin double
HueMax double
SatDev double
SatAvg double
SatMin double
SatMax double
ValDev double
ValAvg double
ValMin double
ValMax double
iPosX int32
iPosY int32
end
end
in function definition below the "Block" argument needs to be of a type "BlockInfo" above so I can access properties of the object
% detecting the blocks with Green Screen hue and
function isGS = DetectGSBlocks(obj, Block)
% Block = BlockInfo;
if 0.39 < Block.HueAvg < 0.43
isGS = 1
end
end
Pretty basic question, I could not find a simple answer for.
Could you please help?
댓글 수: 0
채택된 답변
Walter Roberson
2019년 8월 7일
% detecting the blocks with Green Screen hue and
function isGS = DetectGSBlocks(obj, Block)
assert(isa(Block, 'BlockInfo'))
isGS = 0.39 < Block.HueAvg && Block.HueAvg < 0.43;
end
댓글 수: 9
Steven Lord
2019년 8월 9일
A cat object knows how to meow. Asking a dog object to meow is probably going to get the dog object staring at you confused, since it doesn't know how to meow.
The BlockInfo class does not have a method HasGSColor. Asking it to call GS's HasGSColor method is like asking that dog to meow. If the BlockInfo class has a property that is a GS object, you could call that property's HasGSColor method. The metaphor gets a bit strained here, but if the dog object has a toy object (that contains a cat object that is a stuffed animal) the dog could ask its toy to meow.
meow(mydog.toy) % mydog.toy contains a cat object
Looking more closely, your HasGSColor method doesn't actually do anything with a GS object. That suggests to me that either it should be a Static method with one input that you could call as:
GS.HasGSColor(Block)
or it should be a method of the BlockInfo class (since that's the object on which it acts), or it should be a function (not a method.) If it's needed only inside the BlockInfo class it could be a class-related function defined inside the file but after the classdef block.
추가 답변 (1개)
Guillaume
2019년 8월 7일
If you're asking how to construct a BlockInfo object, with the current class definition, it has to be:
block = BlockInfo;
block.HueDev = ...;
block.HueAvg = ...;
block.HueMin = ...;
%etc.
You could create a constructor to the class definition to help with the creation. However, objects with so many properties are always a pain to construct. If all these properties are scalar, you may want to consider grouping them into vectors.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Call Java from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!