フィルターのクリア

Is there any way to set function (or method) output as comma separated list?

1 回表示 (過去 30 日間)
Hyeokjin Jho
Hyeokjin Jho 2018 年 10 月 24 日
コメント済み: Matt J 2022 年 1 月 12 日
If we have an array of struct, like this:
s(1).a = 1;
s(2).a = 2;
s(3).a = 3;
and then if we get the field of this array, then the result would be like:
s.a
ans =
1
ans =
2
ans =
3
As I know, this output is comma separated list.
I want to have this behavior in my method output too, so that I can use my object just as how I use structs. Something like this:
classdef myObject
properties
myProp
end
methods
function this = myObject(input)
%constructor
this.myProp = input;
end
function output = myMethod(this)
% this function should return myProp as comma separated list
% when myObj array 'this' is given
% I don't know what to do here
% My best try:
% anyway the code below here obviously doesn't work...
output = {this(:).myProp}
end
end
and
myObj(1) = myObject('A');
myObj(2) = myObject('B');
myObj(3) = myObject('C');
myObj.myMethod
ans =
A
ans =
B
ans =
C
It would be cool, isn't it?
I tried varargout, but it only outputs explicit number of nargout, so if the output argument is not specified, it will give only one output, which is varargout{1}.
Is there any good way to do this?
  2 件のコメント
KSSV
KSSV 2018 年 10 月 24 日
YOu make your output a structure and take it out..what issue you have?
Hyeokjin Jho
Hyeokjin Jho 2018 年 10 月 24 日
So the best way here is something like this? :
someStruct = myObj.myMethod;
someStruct.output
ans =
A
ans =
B
ans =
C
and there is no direct way to output a comma separated list?

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

採用された回答

Matt J
Matt J 2018 年 10 月 24 日
編集済み: Matt J 2018 年 10 月 24 日
You should be able to do it by overloading numArgumentsFromSubscript. In other words, tell the class to interpret
>> myObj.myMethod(arg)
as a dot-indexing expression rather than a function call.
The hassle with this is that, similar to overloading subsref, you now have to explicitly implement code in the method to distinguish which dot-indexing expressions are function calls and which are actual indexings.

その他の回答 (1 件)

Hyeokjin Jho
Hyeokjin Jho 2022 年 1 月 12 日
I found a workaround by using properties with custom get method.
classdef myObject
properties
myProp
end
properties (Dependent,SetAccess = private)
myDepProp
end
methods
function this = myObject(input)
%constructor
this.myProp = input;
end
end
methods
function output = get.myDepProp(this)
output = this.myProp; % operates as singleton
end
end
end
Test:
myObj(1) = myObject('A');
myObj(2) = myObject('B');
myObj(3) = myObject('C');
myObj.myDepProp
ans =
'A'
ans =
'B'
ans =
'C'
  2 件のコメント
Matt J
Matt J 2022 年 1 月 12 日
編集済み: Matt J 2022 年 1 月 12 日
Yes, although I think the example would be clearer if MyDepProp performs some additional work that a class method might, e.g.,
methods
function output = get.myDepProp(this)
output = lower(this.myProp); % operates as singleton
end
end
>>myObj(1) = myObject('A');
>>myObj(2) = myObject('B');
>>myObj(3) = myObject('C');
>> myObj.myDepProp
ans =
'a'
ans =
'b'
ans =
'c'
Matt J
Matt J 2022 年 1 月 12 日
A limitation of this approach, of course, is that it does not allow the "method" to take additional input arguments.

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by