Subsref with cells

折月煮酒 提交于 2019-12-04 10:37:55

When the curly braces ({}) are used for indexing a cell array, the output is a comma-separated list. This implicitly calls subsref but the behavior is slightly different from invoking it directly.

subsref by itself is a technically a function, and the comma-separated list returned by the curly braces simply behaves like varargout in this case. This means that you should specify an appropriate "sink" for all desired output results, just like you would do with any other function returning multiple parameters, otherwise they would be ignored.

dont ask me why, this is just something I tried:

myOutput=subsref(myCell,struct('type','()','subs',{{[1 2]}} ) )

note the 'type','()'!

this gives me:

myOutput = 

[2]    [4]

with myOutput as cell. Converting back:

>> myOutput=cell2mat(subsref(myCell,struct('type','()','subs',{{[1 2]}})))

myOutput =

 2     4

This is just a "fast" answer, that will need some improvements or some background-info as well...

I was further investigating @EitanH answer and managed to find more details.

Yeah, it returns a comma separed list, but the function subsref should return a comma separed list just the A{:} does. Here is an example where the functions behave different, but this is an expected behavior, I would like the class .get method to return a list and one class common function to behave as common functions getting only the first argument from the cell.

classdef ListReturn

  properties(Access = private)
    a
  end

  properties(Dependent)
    getA
  end

  methods
    function self = ListReturn(a)
      self.a = a;
    end
    function varargout = get.getA(self)
      varargout = {self.a};
    end
    function varargout = getAFcn(self)
      varargout = {self.a};
    end
  end
end

There is a substantial difference when calling the functions, exactly as the .get:

k=[ListReturn(2) ListReturn(3) ListReturn(4) ListReturn(5)]
>> k.getAFcn

ans =

     2

>> k.getA

ans =

     2


ans =

     3


ans =

     4


ans =

     5

So it seems that using A{1:2} or A{:} works like a Cell.get(), whereas the subsref works as a common matlab function, returning only one argument when the output arguments are not specified, as one function in C,java,C++ would do. Anyway, I just feel like subsref should work as the .get.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!