Giving 'Static Text' a callback

13 visualizaciones (últimos 30 días)
ryan
ryan el 29 de Mayo de 2013
I would like to give a 'static text' in a gui a callback. i.e i would like to be able to click on the static text box, and have that complete a command. Ideally, if there is a way i would like to make a callback for a right click only. Not sure if this is possible but any feedback helps.

Respuesta aceptada

Walter Roberson
Walter Roberson el 29 de Mayo de 2013
In some ways similar to Andrew's suggestion:
You can set HitTest off for the static text box, and then you can define a ButtonDownFcn for the figure (or uipanel as relevant) that detects whether it is over the text and if so does what you want.
Alternately, perhaps defining a uicontext menu for the uicontrol would be suitable for your situation.
  2 comentarios
Sean de Wolski
Sean de Wolski el 29 de Mayo de 2013
This all seems like overkill (and dangerous, what if the text is invisible right now?>, just use the buttondownfcn
Walter Roberson
Walter Roberson el 29 de Mayo de 2013
Note that for uicontrol style text, the buttondownfcn callback will only be invoked for right click (I think it is... control-click on a Mac trackpad.)

Iniciar sesión para comentar.

Más respuestas (2)

Andrew Reibold
Andrew Reibold el 29 de Mayo de 2013
Ok, so what you need to do is make a button instead of a static text box! You can still keep a string of text in it and it can look exactly like a static text box too except maybe it will have a slightly different outline!
There are two properties which can be used in conjunction to accomplish a right click response.
One is the button's 'ButtonDownFcn' callback function. This callback function executes when pressing a mouse button on or near a UICONTROL object -- including when pressing the right mouse button.
The other property is the figure's 'SelectionType' property. This property indicates which kind of click was registered in the figure window -- including clicks on controls within the figure.
Putting these two together, you can define a 'ButtonDownFcn' callback for a push button which checks the figure's 'SelectionType' property to detect a right-click. An example is shown below. (In that example, the ANCESTOR function is used to get the figure's handle. If this is being done in a GUIDE-created GUI, this is unnecessary as the 'handles' structure already provides access to the figure's handle.)
function test
uicontrol('Style', 'pushbutton', ...
'ButtonDownFcn', @myCallback);
end
function myCallback(src, evt)
figHandle = ancestor(src, 'figure');
clickType = get(figHandle, 'SelectionType');
if strcmp(clickType, 'alt')
disp('right click action goes here!');
end
end
  2 comentarios
Sean de Wolski
Sean de Wolski el 29 de Mayo de 2013
This should work with the uicontrol being a textbox too!
etc.
uicontrol('Style', 'text', ...
etc
Walter Roberson
Walter Roberson el 29 de Mayo de 2013
To get rid of the button look itself, you can define its CData property -- possibly even to a rendered version of the text you want to show.

Iniciar sesión para comentar.


ryan
ryan el 30 de Mayo de 2013
@Walter How would you go about changing the CData property?
the only thing with the uicontrol: where do i define the object name (i.e. handles.example)
I have tried this:
set(gca, 'buttondownfcn', @Example_ButtonDownFcn);
and it works. But I am just having trouble with switching out gca with a specified object.

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by