Functions returning part of N-D data

조회 수: 3 (최근 30일)
Thomas
Thomas 2024년 7월 18일
답변: Walter Roberson 2024년 7월 18일
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일
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