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:
Neat Trick, or Dangerous Subversion

Subject: Neat Trick, or Dangerous Subversion

From: Scott

Date: 20 Jul, 2007 18:54:45

Message: 1 of 4

All,

I discovered the following behavior with cells:

>> A = {1,2};
>> A{3:end}

This returns nothing, no message whatsoever, even though it appears to be an index error. Try the same thing with matrices and you'll get an error.

To show how nothing the nothing is, try this:

>> sqrt(4,A{3:end})

ans =

     2

This would give an error "Too many arguments" for anything other than this particular form of nothing.

This looks like a really useful thing when using varargin. I can modify the first argument, then pass the remaining arguments on to another routine,e.g.:

plot(modify(varargin{1}),varargin{2:end})

and not have to worry about if the varargin{2:end} even exists. Otherwise, I'd have to write a separate branch in multiple if statements to handle cases with and without remaining arguments. It's not bad for this example, but you can see how it can cut your labor in two.

My question: I can't find this documented in the MATLAB help. I believe that this little feature is probably used by MATLAB for just the reasons described above, but I don't want design in a bug by using it. Is this feature used elsewhere? Can I count on it?

Scott

Subject: Neat Trick, or Dangerous Subversion

From: Loren Shure

Date: 20 Jul, 2007 15:05:26

Message: 2 of 4

In article <f7r0dl$j7$1@fred.mathworks.com>, millers@yoyodyne.com
says...
> All,
>
> I discovered the following behavior with cells:
>
> >> A = {1,2};
> >> A{3:end}
>
> This returns nothing, no message whatsoever, even though it appears to be an index error.
> Try the same thing with matrices and you'll get an error.
>
> To show how nothing the nothing is, try this:
>
> >> sqrt(4,A{3:end})
>
> ans =
>
> 2
>
> This would give an error "Too many arguments" for anything other than this particular form of nothing.
>
> This looks like a really useful thing when using varargin. I can modify the first argument, then pass
> the remaining arguments on to another routine,e.g.:
>
> plot(modify(varargin{1}),varargin{2:end})
>
> and not have to worry about if the varargin{2:end} even exists. Otherwise, I'd have to write a
> separate branch in multiple if statements to handle cases with and without remaining arguments. It's
> not bad for this example, but you can see how it can cut your labor in two.
>
> My question: I can't find this documented in the MATLAB help. I believe that this little feature is
> probably used by MATLAB for just the reasons described above, but I don't want design in a bug by using it.
> Is this feature used elsewhere? Can I count on it?
>
> Scott
>

This was indeed deliberate for just the use case you mention. When you
end up with an empty cell array, making it into a comma-separated list
gives you nothing.

>> c = {}
c =
     {}
>> c{:}
>>

What exactly do you mean when you say "Try the same thing with matrices
and you'll get an error." ??? What's the code equivalent? I don't
think it's apples to apples but I'd like to be sure there's not a bug
lurking....

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

Subject: Neat Trick, or Dangerous Subversion

From: Scott

Date: 20 Jul, 2007 20:56:27

Message: 3 of 4

Loren Shure <loren@mathworks.com> wrote in message <MPG.210ad180fcfe3d96989777@news.mathworks.com>...
> In article <f7r0dl$j7$1@fred.mathworks.com>, millers@yoyodyne.com
> says...
> > All,
> >
> > I discovered the following behavior with cells:
> >
> > >> A = {1,2};
> > >> A{3:end}
> >
> > This returns nothing, no message whatsoever, even though it appears to be an index error.
> > Try the same thing with matrices and you'll get an error.
> >
> > To show how nothing the nothing is, try this:
> >
> > >> sqrt(4,A{3:end})
> >
> > ans =
> >
> > 2
> >
> > This would give an error "Too many arguments" for anything other than this particular form of nothing.
> >
> > This looks like a really useful thing when using varargin. I can modify the first argument, then pass
> > the remaining arguments on to another routine,e.g.:
> >
> > plot(modify(varargin{1}),varargin{2:end})
> >
> > and not have to worry about if the varargin{2:end} even exists. Otherwise, I'd have to write a
> > separate branch in multiple if statements to handle cases with and without remaining arguments. It's
> > not bad for this example, but you can see how it can cut your labor in two.
> >
> > My question: I can't find this documented in the MATLAB help. I believe that this little feature is
> > probably used by MATLAB for just the reasons described above, but I don't want design in a bug by using it.
> > Is this feature used elsewhere? Can I count on it?
> >
> > Scott
> >
>
> This was indeed deliberate for just the use case you mention. When you
> end up with an empty cell array, making it into a comma-separated list
> gives you nothing.
>
> >> c = {}
> c =
> {}
> >> c{:}
> >>
>
> What exactly do you mean when you say "Try the same thing with matrices
> and you'll get an error." ??? What's the code equivalent? I don't
> think it's apples to apples but I'd like to be sure there's not a bug
> lurking....
>
> --
> Loren
> http://blogs.mathworks.com/loren/

Loren,

Thanks for your response; I'll go ahead and use this feature.

Here's what I meant by equivalent:

>> A=[1 2];
>> A(3:end)

ans =

   Empty matrix: 1-by-0

As you can see, it's one-to-one with the cell example, and the interpreter presents a message (although not an error message).

Scott

Subject: Neat Trick, or Dangerous Subversion

From: Loren Shure

Date: 20 Jul, 2007 17:09:36

Message: 4 of 4

In article <f7r7hr$l14$1@fred.mathworks.com>, millers@yoyodyne.com
says...
> Loren Shure <loren@mathworks.com> wrote in message <MPG.210ad180fcfe3d96989777@news.mathworks.com>...
> > In article <f7r0dl$j7$1@fred.mathworks.com>, millers@yoyodyne.com
> > says...
> > > All,
> > >
> > > I discovered the following behavior with cells:
> > >
> > > >> A = {1,2};
> > > >> A{3:end}
> > >
> > > This returns nothing, no message whatsoever, even though it appears to be an index error.
> > > Try the same thing with matrices and you'll get an error.
> > >
> > > To show how nothing the nothing is, try this:
> > >
> > > >> sqrt(4,A{3:end})
> > >
> > > ans =
> > >
> > > 2
> > >
> > > This would give an error "Too many arguments" for anything other than this particular form of nothing.
> > >
> > > This looks like a really useful thing when using varargin. I can modify the first argument, then pass
> > > the remaining arguments on to another routine,e.g.:
> > >
> > > plot(modify(varargin{1}),varargin{2:end})
> > >
> > > and not have to worry about if the varargin{2:end} even exists. Otherwise, I'd have to write a
> > > separate branch in multiple if statements to handle cases with and without remaining arguments. It's
> > > not bad for this example, but you can see how it can cut your labor in two.
> > >
> > > My question: I can't find this documented in the MATLAB help. I believe that this little feature is
> > > probably used by MATLAB for just the reasons described above, but I don't want design in a bug by using it.
> > > Is this feature used elsewhere? Can I count on it?
> > >
> > > Scott
> > >
> >
> > This was indeed deliberate for just the use case you mention. When you
> > end up with an empty cell array, making it into a comma-separated list
> > gives you nothing.
> >
> > >> c = {}
> > c =
> > {}
> > >> c{:}
> > >>
> >
> > What exactly do you mean when you say "Try the same thing with matrices
> > and you'll get an error." ??? What's the code equivalent? I don't
> > think it's apples to apples but I'd like to be sure there's not a bug
> > lurking....
> >
> > --
> > Loren
> > http://blogs.mathworks.com/loren/
>
> Loren,
>
> Thanks for your response; I'll go ahead and use this feature.
>
> Here's what I meant by equivalent:
>
> >> A=[1 2];
> >> A(3:end)
>
> ans =
>
> Empty matrix: 1-by-0
>
> As you can see, it's one-to-one with the cell example, and the interpreter presents a message (although not an error message).
>
> Scott
>
>

But it's not really 1:1 -- in the cell example, you are pulling out the
contents into a comma-separated list. There's no equivalent for that
with indexing into non-cell arrays. It's intentional that A([])
produces [] for numeric arrays.

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

Tags for 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