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:
How to add a new property to a handle?

Subject: How to add a new property to a handle?

From: Yair Altman

Date: 5 Apr, 2007 15:52:57

Message: 1 of 16

How can I add a new property to a handle-graphics handle (like the
one returned by uicontrol)? For example, adding new property 'MyProp'
so that set or get(h,'MyProp') would work.

I tried using addproperty, but it only works on COM objects.

I tried to use schema.prop(classhandle(handle(h)),'MyProp',...) but
it won't allow me to change an existing class.

I can create a new class (schema.class(findpackage('hg'),'MyClass')
with the existing props and my new prop, but I can't figure out how
to attach it to a new or existing HG handle.

Anyone know how to do this or give me some hint?

Thanks,
Yair Altman

Subject: How to add a new property to a handle?

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 5 Apr, 2007 20:05:40

Message: 2 of 16

In article <ef537ba.-1@webcrossing.raydaftYaTP>,
Yair Altman <altmanyDEL@gmailDEL.comDEL> wrote:

>How can I add a new property to a handle-graphics handle (like the
>one returned by uicontrol)? For example, adding new property 'MyProp'
>so that set or get(h,'MyProp') would work.

Is there a strong reason to do it as a property? Would
setappdata() and kin be sufficient for your purposes?
Or if you need to be able to findobj() on the property, you
could set the property encoded into the Tag and use -regexp to
match the tag prefix.
--
  Is there any thing whereof it may be said, See, this is new? It hath
  been already of old time, which was before us. -- Ecclesiastes

Subject: How to add a new property to a handle?

From: Yair Altman

Date: 5 Apr, 2007 16:41:06

Message: 3 of 16

Walter Roberson wrote:

> Is there a strong reason to do it as a property? Would
> setappdata() and kin be sufficient for your purposes?
> Or if you need to be able to findobj() on the property, you
> could set the property encoded into the Tag and use -regexp to
match the tag prefix.

The reason is that I'm trying to expand uicontrol to accept all sorts
of other GUI elements (like javax.swing.JSpinner etc.). I planned to
use javacomponent(), but this function returns separate handles for
the container and the java object. My plan was to dynamically add the
java object's properties to the Matlab container, and then use
linkprop() to keep these properties synchronized between the handles.
Then the user would be able to set/get JSpinner's properties just
like a togglebutton or an editbox, instead of having to manipulate 2
separate object handles (non-trivial for most Matlab users).
User-friendliness is the keyword here.

I promise to post the result on the File Exchange if enybody could
help me with this...

Yair Altman

Subject: How to add a new property to a handle?

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 5 Apr, 2007 20:54:10

Message: 4 of 16

In article <ef537ba.1@webcrossing.raydaftYaTP>,
Yair Altman <altmanyDEL@gmailDEL.comDEL> wrote:
>The reason is that I'm trying to expand uicontrol to accept all sorts
>of other GUI elements (like javax.swing.JSpinner etc.).

That sounds like you would need not only new properties for uicontrol,
but also new valid Style values.
--
   I was very young in those days, but I was also rather dim.
   -- Christopher Priest

Subject: How to add a new property to a handle?

From: Yair Altman

Date: 5 Apr, 2007 17:28:44

Message: 5 of 16

Walter Roberson wrote:
>> The reason is that I'm trying to expand uicontrol to accept all
sortsof other GUI elements (like javax.swing.JSpinner etc.).
>
> That sounds like you would need not only new properties for
> uicontrol, but also new valid Style values.

Exactly - I planned to add the entire set of Swing elements - if the
user passed a known uicontrol style then the regular (internal)
uicontrol() function would be called. Otherwise, javacomponent would
be called with all the supplied properties. In any case, a single
handle object would be returned to the caller.

A simple logical extension would then be for my new function to
accept ANY java classname inheriting from java.awt.Component (e.g.,
'java.awt.Scrollbar'), but all the famous components would get simple
aliases, so a user could set the style to either
'javax.swing.JTable', 'JTable' or simply 'Table'.

The need for this is that uicontrol() is currently limited to a very
few types of GUI components - many more are available that would be
of much use to the Matlab community (JSpinner, JTable, JTabbedPane,
JSplitPane, JTree etc. etc.). I want a generalized uicontrol() that
would be as transparent as possible to the end user.

The only remaining piece of puzzle is how to unify the properties in
the control's HG container - that's what I'm looking for.

Yair Altman

Subject: How to add a new property to a handle?

From: Yair Altman

Date: 7 Apr, 2007 17:01:37

Message: 6 of 16

I've found out how to add the new properties to a given handle -
simply use schema.prop directly on handle h, not on handle(h). But
unfortunately, the new properties are hidden: they can be read with
get and modified with set, but are not displayed in get(h)/set(h),
nor in inspect(h), not to mention that tab-completion doesn't work
for them. Not even when set(0,'hideundocumented','off').

So, does anyone know how to make schema.props visible?

Subject: How to add a new property to a handle?

From: Yair Altman

Date: 11 Apr, 2007 15:34:00

Message: 7 of 16

I've found out that when you add new properties using schema.prop(),
they are visible in get(handle(h)) but not in get(h), although being
available for use in both cases. So, in my code, I simply expose
handle(h).

Another limitation I've found is that neither inspect(h) nor
inspect(handle(h)) show user-defined properties - only the original
(factory) ones. Not a biggy, this one.

The function (UICOMPONENT) is now ready and posted on the
File-Exchange, as promised: <http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=14583>
 Anyone interested in the details is welcome to look inside this
m-file. In my humble opinion, it's an important enhancement to
uicontrol() & javacomponent(), but I'm biased...

Comments & feedback welcome.

Yair Altman

Subject: How to add a new property to a handle?

From: bourrik

Date: 17 Apr, 2007 13:51:10

Message: 8 of 16

Yair Altman wrote:
>
>
> I've found out that when you add new properties using
> schema.prop(),
> they are visible in get(handle(h)) but not in get(h), although
> being
> available for use in both cases. So, in my code, I simply expose
> handle(h).
>
> Another limitation I've found is that neither inspect(h) nor
> inspect(handle(h)) show user-defined properties - only the original
> (factory) ones. Not a biggy, this one.
>
> The function (UICOMPONENT) is now ready and posted on the
> File-Exchange, as promised: <http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=14583>
> Anyone interested in the details is welcome to look inside this
> m-file. In my humble opinion, it's an important enhancement to
> uicontrol() & javacomponent(), but I'm biased...
>
> Comments & feedback welcome.
>
> Yair Altman

Hi !

You did a very good job on this one ! It's great to be able to add
java components like this.

Now I'd like to add a JTable to a figure, but I can't figure out how
to do it properly. A JTable needs some input args either to define
data and headers, or at least to tell Java how many rows and columns
to display. How do you use such a syntax with uicomponent ?

Thanks and congratulations again !

Subject: How to add a new property to a handle?

From: Yair Altman

Date: 17 Apr, 2007 16:52:22

Message: 9 of 16

bourrik wrote:
> You did a very good job on this one ! It's great to be able to add
java components like this.

Thanks for the compliment. I must say I'm pretty proud of the result
myself...

> Now I'd like to add a JTable to a figure, but I can't figure out
how
> to do it properly. A JTable needs some input args either to define
> data and headers, or at least to tell Java how many rows and
columns
> to display. How do you use such a syntax with uicomponent ?

JTable is indeed one of the more complex elements that you can add.
Normally, you'd want to place a JTable (or JTree etc.) within a
JScrollPane, and place THAT using UICOMPONENT, so that you'd have
default scrollbars etc. In JTable, you also need to take care of the
TableModel, sorting, cell Editors/Renderers etc. etc.

So far for the bad news. The good news is that I've already done all
that: Feel free to download my "Java-based data table" from the File
Exchange: <http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=14225>

If you look at that file's m-code, you'll see many examples of how to
pre-set Java items before the call to UICOMPONENT or JAVACOMPONENT.
My code is basically an enhanced wrapper for Matlab's
undocumented/unsupported UITABLE function.

Comments welcome.

Yair

Subject: How to add a new property to a handle?

From: bourrik

Date: 17 Apr, 2007 18:35:05

Message: 10 of 16

Yair Altman wrote:
>
>
> bourrik wrote:
>> You did a very good job on this one ! It's great to be able to
> add
> java components like this.
>
> Thanks for the compliment. I must say I'm pretty proud of the
> result
> myself...
>
>> Now I'd like to add a JTable to a figure, but I can't figure
out
> how
>> to do it properly. A JTable needs some input args either to
> define
>> data and headers, or at least to tell Java how many rows and
> columns
>> to display. How do you use such a syntax with uicomponent ?
>
> JTable is indeed one of the more complex elements that you can add.
> Normally, you'd want to place a JTable (or JTree etc.) within a
> JScrollPane, and place THAT using UICOMPONENT, so that you'd have
> default scrollbars etc. In JTable, you also need to take care of
> the
> TableModel, sorting, cell Editors/Renderers etc. etc.
>
> So far for the bad news. The good news is that I've already done
> all
> that: Feel free to download my "Java-based data table" from the
> File
> Exchange: <http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=14225>
>
> If you look at that file's m-code, you'll see many examples of how
> to
> pre-set Java items before the call to UICOMPONENT or JAVACOMPONENT.
> My code is basically an enhanced wrapper for Matlab's
> undocumented/unsupported UITABLE function.
>
> Comments welcome.
>
> Yair

Actually I found your other contribution on tables right after having
posted previous reply. I'll take a look at it and give you my
feedback. At first sight, it seems as well documented as uicomponent
and I'm already grateful for that.

Thanks and see you later for comments/remarks

Subject: How to add a new property to a handle?

From: Yair Altman

Date: 19 Apr, 2007 15:54:36

Message: 11 of 16

I was asked the following question by email,which I think is of
general interest so I post it here (with my answer):

    Hi Yair,

    You mentioned that you had found a way to add a new property
'MyProp' to a handle graphics object so that set or get(h,'MyProp')
would work.

    I tried to find out how from your UICOMPONENT code but I'm afraid
most of that Java interaction is over my head. If it's not too much
trouble, could you send me just the snippet of code that would
accomplish this? I'd really appreciate it.

And my answer:

Assume your handle is h, then:

sp = schema.prop(h, 'newPropName', 'mxArray');

% Usage examples
set(h, 'newPropName', magic(3)); % setting new property
get(h, 'newPropName'); % getting new property
get(h); % newPropName will NOT be shown
get(handle(h)); % newPropName WILL be shown
sp = findprop(handle(h),anyPropName); % get the property's
definition

% Special (optional) property processing
sp.DataType = 'bool'; % modify property data type (mxArray is
recommended in most cases)
sp.AccessFlags.PublicSet = 'off'; % make the new prop read-only
(default = 'on')
sp.AccessFlags.PublicGet = 'off'; % make the new prop write-only
(default = 'on')
sp.Visible = 'off'; % hide it from get(handle(h)) - you can still
get/set it, though (default = 'on')
sp.GetFunction = @myGetFunc; % set getter function (default = [])
% Note: func signature is: returnedPropValue = myGetFunc
(object,storedPropValue)
sp.SetFunction = {@mySetFunc,myParam1,myparam2}; % Same for setter
function with 2 optional args
% Note: func signature is: storedPropValue = mySetFunc
(object,proposedPropValue,arg1,arg2) - only first 2 args mandatory
get(sp); % to see everything else that's settable about this prop

Yair Altman

Subject: How to add a new property to a handle?

From: Darik Gamble

Date: 19 Apr, 2007 22:39:54

Message: 12 of 16

Now that I've gotten a chance to play around with this, it seems you
can't set or get the custom properties at the same time as the
default ones, though they seem fine together with findobj. Has this
been your experience as well?

e.g.

h = axes;
schema.prop(h, 'myProp', 'mxArray');

set(h, 'myProp', rand) %No problem

set(h, 'myProp', rand, 'Visible', 'on')

??? Error using ==> set
Invalid axes property: 'myProp'.

This is a pretty minor problem for an incredibly useful feature, but
I was just curious.

Subject: How to add a new property to a handle?

From: Yair Altman

Date: 20 Apr, 2007 03:32:52

Message: 13 of 16

Odd... Let's summarize the known issues with schema.prop so far. If
anyone has more issues or solutions/workaround please post them here:

1. You can't get or set new properties together with original ones,
although you can get/set multiple new props simultaneously if they
are not specified with any original ones:

set(h,'newProp',5,'visible','on'); %fails
set(h,'newProp',5,'newProp2',7); %ok
get(h,{'newProp','visible'}); %fails
get(h,{'newProp','newProp2'}); %ok

2. You can't see new props in get(h) or set(h), although you can see
them in get(handle(h)) or set(handle(h)) [unless of course you set
their sp.Visible to 'off'].

3. You can't see new props (and many original ones as well BTW) in
inspect(h) nor inspect(handle(h)). I suspect this is due to the fact
that there is no java BeanInfo for the inspector to introspect them,
but it's a wild guess and it's no solution anyway.

Yair Altman

Subject: How to add a new property to a handle?

From: us

Date: 20 Apr, 2007 05:51:46

Message: 14 of 16

Yair Altman:
<SNIP <get> conundrum...

we've been using the <schema> stuff for some time and found the
same (unfortunate) problem: the proprietary <get> is not linked
to the stock <get>, as shown in the skeleton below...
we ended up writing out own stock <get>, which chains the
proprietary <get>...

us

% put the function below into <sp.m>
% at the command prompt
     lh=sp('foo',magic(3),'goo','test');
     v1=get(lh,{'foo','goo'}) % ok
     v2=get(lh,{'marker','linestyle'}) % ok
     v3=get(lh,{'foo','marker'}) % error

function lh=sp(varargin)
% note: no error checking...
     narg=fix(.5*nargin);
     lh=line; % just a simple <line> obj
for i=1:narg
     prop=varargin{2*(i-1)+1};
     val=varargin{2*(i-1)+2};
     g=schema.prop(lh,prop,'mxArray');
     set(g,'getfunction',{@myget,prop});
     set(lh,prop,val);
end
function e=myget(h,e,prop)
     disp(sprintf('myget: <%s>',prop));

Subject: How to add a new property to a handle?

From: Thierry

Date: 18 Oct, 2007 11:11:38

Message: 15 of 16

Hello,
I'm using your CreateTable function which is very very
useful, and i have a question to take into account
modifications in the table:
When i edit and change a value in the table, the format of
the value change: it is not java.lang.object but string i
guess. For example, with a table magic(4),
I get the typical following data:
data =


    [16] [ 2] [ 3] [13]
    [ 5] [11] [10] [ 8]
    [ 9] [ 7] [ 6] [12]
    [ 4] [14] [15] [ 1]

Each field is java.lang.object, but if i change for instance
the columns 2 and 3,
data =
    [16] '1' '5' [13]
    [ 5] '2' '5' [ 8]
    [ 9] '3' '5' [12]
    [ 4] '4' '5' [ 1]

I lost the cell format uniformity, and in this case i can't
reuse it to plot columns for example.

How can I change values of my table with keeping the same
format as the unchanged fields?

Thanks in advance for your help.
Thierry

Subject: How to add a new property to a handle?

From: Yair Altman

Date: 18 Oct, 2007 23:16:59

Message: 16 of 16

"Thierry " <titibondat@hotmail.com> wrote in message
<ff7f1a$3rs$1@fred.mathworks.com>...
> Hello,
> I'm using your CreateTable function which is very very
> useful, and i have a question to take into account
> modifications in the table:
> When i edit and change a value in the table, the format of
> the value change: it is not java.lang.object but string i
> guess. For example, with a table magic(4),
> I get the typical following data:
> data =
>
>
> [16] [ 2] [ 3] [13]
> [ 5] [11] [10] [ 8]
> [ 9] [ 7] [ 6] [12]
> [ 4] [14] [15] [ 1]
>
> Each field is java.lang.object, but if i change for instance
> the columns 2 and 3,
> data =
> [16] '1' '5' [13]
> [ 5] '2' '5' [ 8]
> [ 9] '3' '5' [12]
> [ 4] '4' '5' [ 1]
>
> I lost the cell format uniformity, and in this case i can't
> reuse it to plot columns for example.
>
> How can I change values of my table with keeping the same
> format as the unchanged fields?
>
> Thanks in advance for your help.
> Thierry


While your question has nothing to do with this thread
(except perhaps me being the common denominator), here's an
answer:

My createTable, which is based on Matlab's uitable and
ultimately on Java's JTable, uses only 3 basic cell editors:
checkboxes, drop-downs and string labels. If you're certain
the table data is numeric, you can get it as follows (assume
h is your createTable handle):
data=cellfun(@str2num,cellfun(@num2str,cell(h.getData),'un',0));
This will fail in case any of the cells has a non-number.

You can easily set up a customized numeric editor, but you
need to know a bit of Java for this. Here's a link to get
you started:
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#validtext

If you don't want to mess with Java, you can do some
processing in Matlab, within the dataChange_Callback
function, along the following lines (also read createTable's
help section):

function dataChange_Callback(mtable, eventdata)
  if ~ishandle(mtable), return; end
  eventDetails = eventdata.getEvent;
  modifiedCol = eventDetails.getColumn;
  modifiedRow = eventDetails.getFirstRow;
  if modifiedCol>=0 && modifiedRow>=0
    data = mtable.getData;
    newVal = data(modifiedRow+1,modifiedCol+1);
    if ~isnumeric(newVal)
      jTable = mtable.getTable;
      newVal = str2num(newVal); % TODO: sanity checks here
      jtable.setValueAt(newVal,modifiedRow,modifiedCol);
    end
  end
end

If anyone wishes to comment further on this, please start a
new thread. Let's keep this thread for the main topic of
adding props to handles, which is one of the best
undocumented features in Matlab IMHO.

Yair Altman
http://ymasoftware.com

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