Thread Subject: Creating uicontextmenu for copying data to clipboard

Subject: Creating uicontextmenu for copying data to clipboard

From: Johannes Ruhhammer

Date: 9 Jan, 2008 09:25:05

Message: 1 of 4

Hello everyone,

I want to create a uicontextmenu in a gui for copying a
selected lisbox element to clipboard. My code is the
following:

%define context menu
cmenu = uicontextmenu;
%set context menu for listbox element
set(hObject,'UIContextMenu',cmenu);
cb1 = ['copy_selected(hObject)'];
% Define the context menu items
item1 = uimenu(cmenu, 'Label', 'copy', 'Callback', cb1);

The function copy_selected looks like this:

function copy_selected(handles.listbox1)

value = get(handles.listbox1, 'Value');
measurement = get(handles.listbox1,'Sring');
data = measurement{value(1)};
clipboard('copy', data);

I get an error message saying 'undefined Object hObject'.
So my question is how can I pass variables along with the
callback function for the contextmenu?

Thanks for helping,
Rudi.

Subject: Creating uicontextmenu for copying data to clipboard

From: Anh Huy Phan

Date: 9 Jan, 2008 10:42:01

Message: 2 of 4

"Johannes Ruhhammer" <RudiRocker@gmx.net> wrote in message
<fm23th$lqe$1@fred.mathworks.com>...
> Hello everyone,
>
> I want to create a uicontextmenu in a gui for copying a
> selected lisbox element to clipboard. My code is the
> following:
>
> %define context menu
> cmenu = uicontextmenu;
> %set context menu for listbox element
> set(hObject,'UIContextMenu',cmenu);
> cb1 = ['copy_selected(hObject)'];
> % Define the context menu items
> item1 = uimenu(cmenu, 'Label', 'copy', 'Callback', cb1);
>
> The function copy_selected looks like this:
>
> function copy_selected(handles.listbox1)
>
> value = get(handles.listbox1, 'Value');
> measurement = get(handles.listbox1,'Sring');
> data = measurement{value(1)};
> clipboard('copy', data);
>
> I get an error message saying 'undefined Object hObject'.
> So my question is how can I pass variables along with the
> callback function for the contextmenu?
>
> Thanks for helping,
> Rudi.

The error is caused by the header of the function
copy_selected. This function could be as following

function copy_selected(varargin)
hdl = gco;
valfld = get(hdl, 'Value');
strfld = get(hdl,'String');

switch get(hdl,'style')
    case {'listbox' , 'popupmenu'}
        data = strfld{valfld};
    case {'edit', 'text'}
        data = strfld;
    otherwise
        data = valfld;
end
clipboard('copy', data);


Moreover, cb1 needs to be changed.

%define context menu
cmenu = uicontextmenu;

%set context menu for listbox element
%set(hObject,'UIContextMenu',cmenu); %hObject is your listbox
set(handle.listbox1,'UIContextMenu',cmenu);

% Define the context menu items
item1 = uimenu(cmenu, 'Label', 'copy',
'Callback',@copy_selected);


Anh Huy Phan
RIKEN - BSI
   

Subject: Creating uicontextmenu for copying data to clipboard

From: Johannes

Date: 9 Jan, 2008 12:26:02

Message: 3 of 4

Thanks for your help. Everything is now working fine but
cb1 hasn't to be changed because I wrote all in the create
function of the listbox element. So here's my final code:

In the listbox create function:

%define context menu
cmenu = uicontextmenu;
%set context menu for listbox element
set(hObject,'UIContextMenu',cmenu);
% Define the context menu items
item1 = uimenu(cmenu, 'Label', 'copy
selected', 'Callback',@copy_selected);

In copy_selected:
function copy_selected(varargin)
hdl = gco;
valfld = get(hdl, 'Value');
strfld = get(hdl,'String');
data = strfld{valfld};
clipboard('copy', data);

Greetings,
Rudi.

Subject: Creating uicontextmenu for copying data to clipboard

From: Anh Huy Phan

Date: 9 Jan, 2008 13:08:02

Message: 4 of 4

"Johannes " <RudiRocker@gmx.net> wrote in message
<fm2egq$k1a$1@fred.mathworks.com>...
> Thanks for your help. Everything is now working fine but
> cb1 hasn't to be changed because I wrote all in the create
> function of the listbox element. So here's my final code:
>
> In the listbox create function:
>
> %define context menu
> cmenu = uicontextmenu;
> %set context menu for listbox element
> set(hObject,'UIContextMenu',cmenu);
> % Define the context menu items
> item1 = uimenu(cmenu, 'Label', 'copy
> selected', 'Callback',@copy_selected);
>
> In copy_selected:
> function copy_selected(varargin)
> hdl = gco;
> valfld = get(hdl, 'Value');
> strfld = get(hdl,'String');
> data = strfld{valfld};
> clipboard('copy', data);
>
> Greetings,
> Rudi.
>

Right Click on one item of your listbox, and select the
context menu 'copy selected'.

The selected item will be copied to SYSTEM Clipboard.
For checking, you put the console in the command window, and
press CTRL+V, your selected data will be pasted from the
clipboard

or type this command

    clipboard('paste')

Or you can test this function by the following code

figure;
cmenu = uicontextmenu;
item1 = uimenu(cmenu, 'Label', 'copy',
'Callback',@copy_selected);
obj =
uicontrol('style','listbox','string',char(65+(0:5)'),...
    'units','normalized','position',[.1 .1 .3 .5]);
set(obj,'UIContextMenu',cmenu);

By the way, because listbox allows multiselect mode, in this
case, you should convert data variable (cell) to a string by
mat2str or sprintf, cellfun

Anh Huy Phan
RIKEN - BSI

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
uicontextmenu Johannes 9 Jan, 2008 04:30:08
clipboard Johannes 9 Jan, 2008 04:30:08
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com