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!