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:
Please TMW WTH is this?

Subject: Please TMW WTH is this?

From: Budias Aao

Date: 10 Jul, 2012 01:56:35

Message: 1 of 10


h=patch('XData',rand(5,1),'YData',rand(5,1));
hold on
h2=patch('XData',rand(5,1),'YData',rand(5,1));
getappdata(h2)

ans =

1x1 struct array with no fields.

>> isempty(getappdata(h2))

ans =

     0

Subject: Please TMW WTH is this?

From: Barry Williams

Date: 10 Jul, 2012 10:31:10

Message: 2 of 10

"Budias Aao" wrote in message <jtg24j$e9i$1@newscl01ah.mathworks.com>...
>
> h=patch('XData',rand(5,1),'YData',rand(5,1));
> hold on
> h2=patch('XData',rand(5,1),'YData',rand(5,1));
> getappdata(h2)
>
> ans =
>
> 1x1 struct array with no fields.
>
> >> isempty(getappdata(h2))
>
> ans =
>
> 0

doc setappdata: setappdata(h,'name',value) sets application-defined data for the object with handle h.
doc getappdata: If the application-defined data does not exist, MATLAB returns an empty matrix in value.
In other words, the appdata needs to be set first before you can get it.

Were you perhaps looking for:
                        values = get(h2); % Gets all properties of a graphics object.

Barry

Subject: Please TMW WTH is this?

From: Budias Aao

Date: 10 Jul, 2012 13:03:07

Message: 3 of 10


> > >> isempty(getappdata(h2))
> >
> > ans =
> >
> > 0
>
> doc setappdata: setappdata(h,'name',value) sets application-defined data for the object with handle h.
> doc getappdata: If the application-defined data does not exist, MATLAB returns an empty matrix in value.

That is precisely the point. It should return an empty matrix but it doesn't. It returns a new abstraction thing that is a

"1x1 struct array with no fields."

but that is NOT empty

isempty(getappdata(h2))

ans = 0

It's nasty BUG, that's what it is.

Subject: Please TMW WTH is this?

From: Steven_Lord

Date: 10 Jul, 2012 13:28:38

Message: 4 of 10



"Budias Aao" <budiao@hotmail.com> wrote in message
news:jth96b$rod$1@newscl01ah.mathworks.com...
>
>> > >> isempty(getappdata(h2))
>> >
>> > ans =
>> >
>> > 0
>>
>> doc setappdata: setappdata(h,'name',value) sets application-defined data
>> for the object with handle h. doc getappdata: If the application-defined
>> data does not exist, MATLAB returns an empty matrix in value.
>
> That is precisely the point. It should return an empty matrix but it
> doesn't. It returns a new abstraction thing that is a
>
> "1x1 struct array with no fields."
>
> but that is NOT empty
>
> isempty(getappdata(h2))
>
> ans = 0
>
> It's nasty BUG, that's what it is.

This is NOT a bug. Let's take a closer look at the reference page for
GETAPPDATA:

http://www.mathworks.com/help/techdoc/ref/getappdata.html

The line you quoted above about GETAPPDATA returning an empty matrix holds
for the TWO input argument syntax for GETAPPDATA. But you're not calling
GETAPPDATA with two input arguments, you're calling it with one. The section
of the documentation dealing with that case is:

"values = getappdata(h) returns all application-defined data for the object
with handle h. "

Since application data consists of values that are each associated with a
name, the obvious choice of data structure for GETAPPDATA to return in this
situation is a scalar struct array. [It should be scalar since h2 is
scalar.] Since you haven't called SETAPPDATA on h2, though, there IS no
application data and so that struct array will have no fields, just like
this one:

    s = struct()

Since the struct array is scalar its size is [1 1] and by the definition in
the documentation for ISEMPTY:

http://www.mathworks.com/help/techdoc/ref/isempty.html

"An empty array has at least one dimension of size zero, for example, 0-by-0
or 0-by-5."

it is not empty. Therefore isempty(getappdata(h2)) returning false is the
correct behavior.

If you want to use the one input syntax to determine if the object has any
application data set on it, use:

    isempty(fieldnames(getappdata(h2)))

Since the struct array has no fields, FIELDNAMES will return an empty cell
array and that IS empty by the definition quoted above.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Please TMW WTH is this?

From: Budias Aao

Date: 10 Jul, 2012 14:01:20

Message: 5 of 10

 
> "values = getappdata(h) returns all application-defined data for the object
> with handle h. "
>
> Since application data consists of values that are each associated with a
> name, the obvious choice of data structure for GETAPPDATA to return in this
> situation is a scalar struct array. [It should be scalar since h2 is
> scalar.] Since you haven't called SETAPPDATA on h2, though, there IS no
> application data and so that struct array will have no fields, just like
> this one:
>
> s = struct()

OK, thanks.

But why did that change?
R13 returns an empty matrix as it should (and is explained clearly in the docs).
I find the current behaviour most counter intuitive and worst. It breaks compatibility, which is something that TMW unfortunately does not really care much.

Subject: Please TMW WTH is this?

From: Steven_Lord

Date: 10 Jul, 2012 15:11:50

Message: 6 of 10



"Budias Aao" <budiao@hotmail.com> wrote in message
news:jthcjg$d6k$1@newscl01ah.mathworks.com...
>
>> "values = getappdata(h) returns all application-defined data for the
>> object with handle h. "
>>
>> Since application data consists of values that are each associated with a
>> name, the obvious choice of data structure for GETAPPDATA to return in
>> this situation is a scalar struct array. [It should be scalar since h2 is
>> scalar.] Since you haven't called SETAPPDATA on h2, though, there IS no
>> application data and so that struct array will have no fields, just like
>> this one:
>>
>> s = struct()
>
> OK, thanks.
>
> But why did that change?
> R13 returns an empty matrix as it should (and is explained clearly in the
> docs).

Can you show the EXACT code you're running where you see that? That's not
what I see when I try an example in release R13, nor is that what I'd expect
from the help. Again, don't confuse the output from the two-input syntax
with the output from the one-input syntax.

>> version
ans =
6.5.0.180913a (R13)
>> help getappdata

 GETAPPDATA Get value of application-defined data.
   VALUE = GETAPPDATA(H, NAME) gets the value of the
   application-defined data with name specified by NAME in the
   object with handle H. If the application-defined data does
   not exist, an empty matrix will be returned in VALUE.

   VALUES = GETAPPDATA(H) returns all application-defined data
   for the object with handle H.

   See also SETAPPDATA, RMAPPDATA, ISAPPDATA.

>> h2=patch('XData',rand(5,1),'YData',rand(5,1));
>> q = getappdata(h2);
>> class(q)
ans =
struct
>> size(q)
ans =
     1 1
>> isempty(fieldnames(q))
ans =
     1
>> isempty(q)
ans =
     0

GETAPPDATA returns a scalar struct array with no fields. It is not empty. If
this is not what you're seeing, execute all those commands and show the
output you see. [You should also post the output of "which -all getappdata"
to make sure nothing's shadowing the version included with MATLAB.]

> I find the current behaviour most counter intuitive and worst. It breaks
> compatibility, which is something that TMW unfortunately does not really
> care much.

We care a LOT about compatibility. [As someone who's been involved in plenty
of discussions here about compatibility, and who occasionally initiates
those discussions, trust me we care.]

Do we always choose to maintain compatibility? No. But in general we
consider very carefully _if_ we should break compatibility, _when_ we should
do so, and _how_ we should do so. We don't always make the decisions our
users may expect, nor do we always get it right. But we don't say "It's a
Thursday, let's break backwards compatibility for fun."

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Please TMW WTH is this?

From: Budias Aao

Date: 10 Jul, 2012 15:47:10

Message: 7 of 10

 
> Can you show the EXACT code you're running where you see that? That's not
> what I see when I try an example in release R13, nor is that what I'd expect
> from the help. Again, don't confuse the output from the two-input syntax
> with the output from the one-input syntax.

The one input only serves at the purpose just knowing if there is something in appdata. At that point I don't care what's inside appdata. Only if it exists, case in which I copy it, or not. So the one input only is quite legitimate and on R13. Unfortunately that's not what more recent versions report. Saying the it is not empty because it has an empty box inside doesn't really help much.

>> h=patch('XData',rand(5,1),'YData',rand(5,1));
>> getappdata(h)

ans =


>> version

ans =

6.5.0.180913a (R13)

Subject: Please TMW WTH is this?

From: Budias Aao

Date: 10 Jul, 2012 16:02:11

Message: 8 of 10

"Budias Aao" wrote in message <jthipu$9ul$1@newscl01ah.mathworks.com>...
>
> > Can you show the EXACT code you're running where you see that? That's not
> > what I see when I try an example in release R13, nor is that what I'd expect
> > from the help. Again, don't confuse the output from the two-input syntax
> > with the output from the one-input syntax.
>
> The one input only serves at the purpose just knowing if there is something in appdata. At that point I don't care what's inside appdata. Only if it exists, case in which I copy it, or not. So the one input only is quite legitimate and on R13. Unfortunately that's not what more recent versions report. Saying the it is not empty because it has an empty box inside doesn't really help much.
>
> >> h=patch('XData',rand(5,1),'YData',rand(5,1));
> >> getappdata(h)
>
> ans =
>
>
> >> version
>
> ans =
>
> 6.5.0.180913a (R13)

OK,

Indeed it's not empty either on R13 but the way it prints the variable content had made think it was empty (as, again, I think would be the logic behavior)

>> q=getappdata(h)

q =


>> isempty(q)

ans =

     0

Subject: Please TMW WTH is this?

From: Steven_Lord

Date: 10 Jul, 2012 17:41:38

Message: 9 of 10



"Budias Aao" <budiao@hotmail.com> wrote in message
news:jthjm3$dhb$1@newscl01ah.mathworks.com...
> "Budias Aao" wrote in message <jthipu$9ul$1@newscl01ah.mathworks.com>...
>>
>> > Can you show the EXACT code you're running where you see that? That's
>> > not what I see when I try an example in release R13, nor is that what
>> > I'd expect from the help. Again, don't confuse the output from the
>> > two-input syntax with the output from the one-input syntax.
>>
>> The one input only serves at the purpose just knowing if there is
>> something in appdata. At that point I don't care what's inside appdata.
>> Only if it exists, case in which I copy it, or not. So the one input only
>> is quite legitimate and on R13. Unfortunately that's not what more recent
>> versions report. Saying the it is not empty because it has an empty box
>> inside doesn't really help much.
>>
>> >> h=patch('XData',rand(5,1),'YData',rand(5,1));
>> >> getappdata(h)
>>
>> ans =
>> >> version
>>
>> ans =
>>
>> 6.5.0.180913a (R13)
>
> OK,
>
> Indeed it's not empty either on R13 but the way it prints the variable
> content had made think it was empty (as, again, I think would be the logic
> behavior)

Ah, I see. [Or rather I don't, which is the problem.] Apparently we did
change the way struct arrays with no fields are displayed between release
R13 and R2012a. Based on my experience my guess is that change was
deliberate, prompted by a user reporting that lack of display as a bug. IMO
the increased usability of the display (explicitly stating that the struct
array has no fields instead of showing nothing at all) outweighs the
incompatibility caused by the change in how the struct is displayed.

I prefer that GETAPPDATA returns a scalar struct array in all cases. IMO I
find that logical and the fewer special case rules there are the easier it
is to remember the behavior of the function. The rule "the output of
GETAPPDATA is a scalar struct array with one field for each piece of
application data in the object" has fewer special cases than "the output of
GETAPPDATA is a scalar struct array with one field for each piece of
application data in the object, except if the object has no application data
in which case it's an empty struct array." MATLAB is complicated enough as
it is! ;)

Getting back to a comment you made at the beginning of your previous reply,
since you said that you are just using this to try to copy the appdata over
there's a way to do this without special casing whether or not the
application data is empty. You don't really care how many pieces of
application data there are, you just want to copy them all over. So do that
with something like this (untested, but it should work. You may need to
transpose the list of fieldnames; I don't remember off the top of my head if
it returns a row or column cell array.)

q = getappdata(h1);
for thefields = fieldnames(q)
    currentFieldName = thefields{1};
    % if dynamic field names are available (I don't think they are in
release R13 but they are in later releases), use that instead of GETFIELD
    currentFieldValue = getfield(q, currentFieldName);
    setappdata(h2, currentFieldName, currentFieldValue);
end

If q is a struct with no fields, the FOR loop will execute once for each of
the 0 elements in fieldnames(q) (i.e. not at all) while if it has fields it
will execute once per field.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Please TMW WTH is this?

From: Barry Williams

Date: 10 Jul, 2012 18:10:20

Message: 10 of 10

"Steven_Lord" <slord@mathworks.com> wrote in message <jtham6$4os$1@newscl01ah.mathworks.com>...
>
>
> "Budias Aao" <budiao@hotmail.com> wrote in message
> news:jth96b$rod$1@newscl01ah.mathworks.com...
> >
> >> > >> isempty(getappdata(h2))
> >> >
> >> > ans =
> >> >
> >> > 0
> >>
> >> doc setappdata: setappdata(h,'name',value) sets application-defined data
> >> for the object with handle h. doc getappdata: If the application-defined
> >> data does not exist, MATLAB returns an empty matrix in value.
> >
> > That is precisely the point. It should return an empty matrix but it
> > doesn't. It returns a new abstraction thing that is a
> >
> > "1x1 struct array with no fields."
> >
> > but that is NOT empty
> >
> > isempty(getappdata(h2))
> >
> > ans = 0
> >
> > It's nasty BUG, that's what it is.
>
> This is NOT a bug. Let's take a closer look at the reference page for
> GETAPPDATA:
>
> http://www.mathworks.com/help/techdoc/ref/getappdata.html
>
> The line you quoted above about GETAPPDATA returning an empty matrix holds
> for the TWO input argument syntax for GETAPPDATA. But you're not calling
> GETAPPDATA with two input arguments, you're calling it with one. The section
> of the documentation dealing with that case is:
>
> "values = getappdata(h) returns all application-defined data for the object
> with handle h. "
>
> Since application data consists of values that are each associated with a
> name, the obvious choice of data structure for GETAPPDATA to return in this
> situation is a scalar struct array. [It should be scalar since h2 is
> scalar.] Since you haven't called SETAPPDATA on h2, though, there IS no
> application data and so that struct array will have no fields, just like
> this one:
>
> s = struct()
>
> Since the struct array is scalar its size is [1 1] and by the definition in
> the documentation for ISEMPTY:
>
> http://www.mathworks.com/help/techdoc/ref/isempty.html
>
> "An empty array has at least one dimension of size zero, for example, 0-by-0
> or 0-by-5."
>
> it is not empty. Therefore isempty(getappdata(h2)) returning false is the
> correct behavior.
>
> If you want to use the one input syntax to determine if the object has any
> application data set on it, use:
>
> isempty(fieldnames(getappdata(h2)))
>
> Since the struct array has no fields, FIELDNAMES will return an empty cell
> array and that IS empty by the definition quoted above.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

You're correct, Steve, and I did not mean to state that it is empty.
Barry

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