Functions returning part of N-D data

17 次查看(过去 30 天)
Thomas
Thomas 2024-7-18,18:56
回答: Walter Roberson 2024-7-18,20:04
I made a class that has methods to return specific slices of an n-d array. I can use these methods as if they are properties, and subindexing works with one exception, I can't index with just a colon, I have to enclose it in quotes.
Here are examples where normal indexing works:
>> foobar = cfoobar(reshape(1:8,2,2,2))
foobar =
cfoobar with no properties.
>> foobar.foo([2,1],[2,1])
ans =
4 2
3 1
>> foobar.bar(1:end,end)
ans =
7
8
Here is the example that doesn't work:
>> foobar.bar(:,end)
Input arguments to function include colon operator. To input the colon character, use ':' instead.
Here is the example working with quotes:
>> foobar.bar(':',end)
ans =
7
8
>>
Here is the class:
classdef cfoobar
properties (Access = private)
foobar;
end
methods
function obj = cfoobar(data)
obj.foobar = data;
end
function data = foo(obj, varargin)
data = obj.foobar(:,:,1);
data = data(varargin{:});
end
function data = bar(obj, varargin)
data = obj.foobar(:,:,2);
data = data(varargin{:});
end
end
end
How do I write the class to make the colon work by itself?

回答(1 个)

Walter Roberson
Walter Roberson 2024-7-18,20:04
How do I write the class to make the colon work by itself?
You would have to make bar a property instead of a method. Once it is a property, normal indexing would work.

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by