How to specify the type of the argument of the function?

2 ビュー (過去 30 日間)
Hakob Bazoyan
Hakob Bazoyan 2019 年 8 月 7 日
コメント済み: Hakob Bazoyan 2019 年 8 月 9 日
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?

採用された回答

Walter Roberson
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
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.
Hakob Bazoyan
Hakob Bazoyan 2019 年 8 月 9 日
Steven, Guillaume,
Thanks much for help, I think I know how to fix the problem.

サインインしてコメントする。

その他の回答 (1 件)

Guillaume
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.

カテゴリ

Help Center および File ExchangeCall Java from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by