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:
Can Someone Explain this Matlab Behavior?

Subject: Can Someone Explain this Matlab Behavior?

From: Zampano

Date: 28 Jan, 2011 21:29:24

Message: 1 of 5

Matlab allows me to call the size function without parentheses. When
I do, I get unexpected results. See the example below. What's going
on? I'm using Matlab R2010a on a Mac.

Rick

>> r = [1 2 3]

r =

     1 2 3

>> size r

ans =

     1 1

>> size(r)

ans =

     1 3

>> r2 = size r
??? r2 = size r
              |
Error: Unexpected MATLAB expression.

Subject: Can Someone Explain this Matlab Behavior?

From: ImageAnalyst

Date: 28 Jan, 2011 21:36:53

Message: 2 of 5

On Jan 28, 4:29 pm, Zampano <zacc...@gmail.com> wrote:
> Matlab allows me to call the size function without parentheses.  When
> I do, I get unexpected results.  See the example below.  What's going
> on?  I'm using Matlab R2010a on a Mac.
>
> Rick
>
> >> r = [1 2 3]
>
> r =
>
>      1     2     3
>
> >> size r
>
> ans =
>
>      1     1
>
> >> size(r)
>
> ans =
>
>      1     3
>
> >> r2 = size r
>
> ??? r2 = size r
>               |
> Error: Unexpected MATLAB expression.

--------------------------------------------------------
When you say size r you're getting the size of the letter r as if it's
a literal string. When you say size(r) you're getting the size of
your r variable array. If you want to assign it to anything, such as
r2, you need to use the function form r2=size(r). There is a web page
somewhere that describes how inputs are characters when you use the
script form of calling functions instead of the functional form.

Subject: Can Someone Explain this Matlab Behavior?

From: Alan B

Date: 28 Jan, 2011 21:41:04

Message: 3 of 5

Zampano <zaccone@gmail.com> wrote in message <a6cbf5c8-fbfc-4683-9ca0-8522bcbb2c17@o39g2000prb.googlegroups.com>...
> Matlab allows me to call the size function without parentheses. When
> I do, I get unexpected results. See the example below. What's going
> on? I'm using Matlab R2010a on a Mac.
>
> Rick
>
> >> r = [1 2 3]
>
> r =
>
> 1 2 3
>
> >> size r
>
> ans =
>
> 1 1
>
> >> size(r)
>
> ans =
>
> 1 3
>
> >> r2 = size r
> ??? r2 = size r
> |
> Error: Unexpected MATLAB expression.

That's called "command syntax", as opposed to "function syntax". It's like a Unix commands - the arguments are space-delimited strings, instead of comma-delimited expressions. No result is returned, so trying to assign the nonexistent output to a variable gives an error.

size r
is equivalent to
size('r')

Subject: Can Someone Explain this Matlab Behavior?

From: Zampano

Date: 29 Jan, 2011 10:43:56

Message: 4 of 5

On Jan 28, 4:41 pm, "Alan B" <monguin61...@OVETHIS.yahoo.com> wrote:
> Zampano <zacc...@gmail.com> wrote in message <a6cbf5c8-fbfc-4683-9ca0-8522bcbb2...@o39g2000prb.googlegroups.com>...
> > Matlab allows me to call the size function without parentheses.  When
> > I do, I get unexpected results.  See the example below.  What's going
> > on?  I'm using Matlab R2010a on a Mac.
>
> > Rick
>
> > >> r = [1 2 3]
>
> > r =
>
> >      1     2     3
>
> > >> size r
>
> > ans =
>
> >      1     1
>
> > >> size(r)
>
> > ans =
>
> >      1     3
>
> > >> r2 = size r
> > ??? r2 = size r
> >               |
> > Error: Unexpected MATLAB expression.
>
> That's called "command syntax", as opposed to "function syntax". It's like a Unix commands - the arguments are space-delimited strings, instead of comma-delimited expressions. No result is returned, so trying to assign the nonexistent output to a variable gives an error.
>
> size r
> is equivalent to
> size('r')

Thanks. I wasn't aware that Matlab had command and function syntax.
I have since found the following link that explains this.

http://www.mathworks.com/help/techdoc/matlab_prog/f7-58170.html

I have been using Matlab for years and I've been unaware of this
issue. It could be very confusing for a newbie.

Subject: Can Someone Explain this Matlab Behavior?

From: Jan Simon

Date: 29 Jan, 2011 14:19:03

Message: 5 of 5

Dear Zampano,

> I have been using Matlab for years and I've been unaware of this
> issue. It could be very confusing for a newbie.

It is confusing for experienced programmers also. You can find a pile of questions regarding "save FileName" and "save(FileName)" in CSSM, although this is explicitely explained in HELP SAVE.
Also confusing: there is no functional form of e.g. GLOBAL.
And the automatic conversion changes with the Matlab releases:
Matlab 6.5:
  fullfile * *
  >> *\*
  fullfile * p
  >> *\p
Matlab 2009a:
  fullfile * *
  >> *\*
  fullfile * p
  >> Error using ==> fullfile. Not enough input arguments
One of the very rare examples of absolutely useless error messages from Matlab.

You find gimicks like "clear(z)" in a lot of example codes, but in most cases the user does not want to clear the variable whos name is stored in the string z.

Kind regards, Jan

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