Borrar filtros
Borrar filtros

Functions returning part of N-D data

17 visualizaciones (últimos 30 días)
Thomas
Thomas el 18 de Jul. de 2024 a las 18:56
Respondida: Walter Roberson el 18 de Jul. de 2024 a las 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?

Respuestas (1)

Walter Roberson
Walter Roberson el 18 de Jul. de 2024 a las 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.

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by