Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
What function to overload for plain : operator?

Subject: What function to overload for plain : operator?

From: Hoi Wong

Date: 28 Mar, 2009 22:47:02

Message: 1 of 6

I'm trying to write my own sparse_cell class and I noticed that the s.subs for subsref and subsasgn does not process a plain colon (:) operator into a vector. It only showed up as ':', which means I have to consider non-vector references. It's so confusing that the co-author initially overlooked this case and have ':' evaluated as 58 as index and the class generated legitimate but wrong output.

I tried to overload the colon operator, but unlike end(), it never gets called when I type in x(:). Is there a way that I can turn ":" into "1:end" and make sure if x(:) outputs a column and x(:,:) outputs x?

Thanks in advance for any help :)

Subject: What function to overload for plain : operator?

From: Donn Shull

Date: 29 Mar, 2009 02:20:02

Message: 2 of 6

"Hoi Wong" <wonghoi.ee@gmailNOSPAM.com> wrote in message <gqm9d6$ch5$1@fred.mathworks.com>...
> I'm trying to write my own sparse_cell class and I noticed that the s.subs for subsref and subsasgn does not process a plain colon (:) operator into a vector. It only showed up as ':', which means I have to consider non-vector references. It's so confusing that the co-author initially overlooked this case and have ':' evaluated as 58 as index and the class generated legitimate but wrong output.
>
> I tried to overload the colon operator, but unlike end(), it never gets called when I type in x(:). Is there a way that I can turn ":" into "1:end" and make sure if x(:) outputs a column and x(:,:) outputs x?
>
> Thanks in advance for any help :)

I think that what you want to do is write a method for your sparse_cell class which overloads the colon operator.

Good Luck,

Donn

Subject: What function to overload for plain : operator?

From: Hoi Wong

Date: 29 Mar, 2009 02:48:01

Message: 3 of 6

"Donn Shull" <donn.shull.no_spam@aetoolbox.com> wrote in message <gqmlsi$i68$1@fred.mathworks.com>...
> "Hoi Wong" <wonghoi.ee@gmailNOSPAM.com> wrote in message <gqm9d6$ch5$1@fred.mathworks.com>...
> > I'm trying to write my own sparse_cell class and I noticed that the s.subs for subsref and subsasgn does not process a plain colon (:) operator into a vector. It only showed up as ':', which means I have to consider non-vector references. It's so confusing that the co-author initially overlooked this case and have ':' evaluated as 58 as index and the class generated legitimate but wrong output.
> >
> > I tried to overload the colon operator, but unlike end(), it never gets called when I type in x(:). Is there a way that I can turn ":" into "1:end" and make sure if x(:) outputs a column and x(:,:) outputs x?
> >
> > Thanks in advance for any help :)
>
> I think that what you want to do is write a method for your sparse_cell class which overloads the colon operator.
>
> Good Luck,
>
> Donn

Thanks Donn, I tried to overload colon operator already. It didn't even get to they keyboard statement placed in the first line. Note that things behaves akward only for (:), but unfortunately that's the one I used most often.

Subject: What function to overload for plain : operator?

From: Loren Shure

Date: 30 Mar, 2009 14:23:55

Message: 4 of 6

In article <gqm9d6$ch5$1@fred.mathworks.com>, wonghoi.ee@gmailNOSPAM.com
says...
> I'm trying to write my own sparse_cell class and I noticed that the s.subs for subsref and subsasgn does not process a plain colon (:) operator into a vector. It only showed up as ':', which means I have to consider non-vector references. It's so confusing that the co-author initially overlooked this case and have ':' evaluated as 58 as index and the class generated legitimate but wrong output.
>
> I tried to overload the colon operator, but unlike end(), it never gets called when I type in x(:). Is there a way that I can turn ":" into "1:end" and make sure if x(:) outputs a column and x(:,:) outputs x?
>
> Thanks in advance for any help :)
>

The subsref and subsasgn methods you write need to look for ':' and
handle it as one of the cases.

--
Loren
http://blogs.mathworks.com/loren

Subject: What function to overload for plain : operator?

From: Steven Lord

Date: 30 Mar, 2009 17:19:06

Message: 5 of 6


"Hoi Wong" <wonghoi.ee@gmailNOSPAM.com> wrote in message
news:gqm9d6$ch5$1@fred.mathworks.com...
> I'm trying to write my own sparse_cell class and I noticed that the s.subs
> for subsref and subsasgn does not process a plain colon (:) operator into
> a vector. It only showed up as ':', which means I have to consider
> non-vector references. It's so confusing that the co-author initially
> overlooked this case and have ':' evaluated as 58 as index and the class
> generated legitimate but wrong output.
>
> I tried to overload the colon operator, but unlike end(), it never gets
> called when I type in x(:). Is there a way that I can turn ":" into
> "1:end" and make sure if x(:) outputs a column and x(:,:) outputs x?
>
> Thanks in advance for any help :)

The COLON method is not called in indexing; it's used when you create an
expression like:

a = myObjectThatOverloadsColon;
y = 1:2:a;

% or

a = timeseriesDataStorage('01/01/2009', dataForJan1);
b = timeseriesDataStorage('03/01/2009', dataForMar1);
interpolatedData = a:1:b;

Note: I do not have code for either of those classes; I simply used the
names for demonstration purposes.

You will need to have your SUBSREF and SUBSASGN methods check if the subs
field of the input substruct contains ':' and expand it to be the vector
1:size(theobject, appropriateDimension) -- or if you can iterate over the
object referencing into it or assinging to it without explicitly creating
the index vector, that would work too.

--
Steve Lord
slord@mathworks.com

Subject: What function to overload for plain : operator?

From: Hoi Wong

Date: 30 Mar, 2009 18:12:02

Message: 6 of 6

"Steven Lord" <slord@mathworks.com> wrote in message <gqqutf$lnh$1@fred.mathworks.com>...
>
> "Hoi Wong" <wonghoi.ee@gmailNOSPAM.com> wrote in message
> news:gqm9d6$ch5$1@fred.mathworks.com...
> > I'm trying to write my own sparse_cell class and I noticed that the s.subs
> > for subsref and subsasgn does not process a plain colon (:) operator into
> > a vector. It only showed up as ':', which means I have to consider
> > non-vector references. It's so confusing that the co-author initially
> > overlooked this case and have ':' evaluated as 58 as index and the class
> > generated legitimate but wrong output.
> >
> > I tried to overload the colon operator, but unlike end(), it never gets
> > called when I type in x(:). Is there a way that I can turn ":" into
> > "1:end" and make sure if x(:) outputs a column and x(:,:) outputs x?
> >
> > Thanks in advance for any help :)
>
> The COLON method is not called in indexing; it's used when you create an
> expression like:
>
> a = myObjectThatOverloadsColon;
> y = 1:2:a;
>
> % or
>
> a = timeseriesDataStorage('01/01/2009', dataForJan1);
> b = timeseriesDataStorage('03/01/2009', dataForMar1);
> interpolatedData = a:1:b;
>
> Note: I do not have code for either of those classes; I simply used the
> names for demonstration purposes.
>
> You will need to have your SUBSREF and SUBSASGN methods check if the subs
> field of the input substruct contains ':' and expand it to be the vector
> 1:size(theobject, appropriateDimension) -- or if you can iterate over the
> object referencing into it or assinging to it without explicitly creating
> the index vector, that would work too.
>
> --
> Steve Lord
> slord@mathworks.com
>

Thanks Steve and Loren. In fact that's what I did in my code. Whenever I cannot pass the buck to the next function that accepts ':', I use

colonIndicator=strcmp(':', s.subs);
dimensionLengths=size(a);
fullIndex=arrayfun(@(L)1:L, s.subs, 'UniformOutput', false);
s.subs(colonIndicator)=fullIndex(colonIndicator);

and for some reason 1:L is slow when L is giant (which is my case), so I might end up using cumsum(ones(L,1)) instead, but it's getting messier. I wonder if it's a good idea to overload the colon operator in general (double class) that whenever it sees 1:L or 1:1:L, it automatically does cumsum() instead?

Also, the default behavior of (:) generates column output, but the code above won't handle that, so I also need to check if( isequal(colonIndicator, ':') ) as well. IMHO, subsasgn() is the most dangerous operator to overload as it can corrupt data, so the messier the code is, the more likely that disasters like ':' operator mishandled as [58] and served as index happens. I wonder if there's a cleaner way to do it.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us