Clear Filters
Clear Filters

Functions returning part of N-D data

17 views (last 30 days)
Thomas
Thomas on 18 Jul 2024 at 18:56
Answered: Walter Roberson on 18 Jul 2024 at 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?

Answers (1)

Walter Roberson
Walter Roberson on 18 Jul 2024 at 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.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!