Azzera filtri
Azzera filtri

Functions returning part of N-D data

17 visualizzazioni (ultimi 30 giorni)
Thomas
Thomas il 18 Lug 2024 alle 18:56
Risposto: Walter Roberson il 18 Lug 2024 alle 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?

Risposte (1)

Walter Roberson
Walter Roberson il 18 Lug 2024 alle 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.

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Prodotti


Release

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by