MATLAB: Perform callback on variable change? - matlab

I am trying to build a GUI with several tabs, using uitabpanel() found at http://www.mathworks.com/matlabcentral/fileexchange/11546 . I would like to resize the GUI based on the currently opened tab; this is available with uitabpanel.SelectedItem.
Basically I would like to build a callback in order to monitor uitabpanel.SelectedItem - when this variable changes, the GUI window should resize appropriately. Generally speaking, I am looking for a way to monitor a variable and execute a callback when the variable changes value.
Is this possible? How would I go about doing this?
Thanks!

I don't have a MATLAB in front of me right now, but if it is implemented as a property -- and as far as I can tell from a quick look at the code, it is -- you can use addlistener function and provide a callback function for it.
addlistener(hTab,'SelectedItem','PostSet',#(s,e)disp('SelectedItem changed'))

I'm not familiar with uitab from the file exchange. However, if it's build upon the built-in uitab, then there should be a selectionChangeCallback or selectionChangeFcn property (depending on your Matlab version). Specify a function for this callback property, and you have a way to execute a function whenever the selection changes.
If that's not possible, the only other way to monitor a variable change (if you can't somehow use objects and set methods) is to use a TIMER OBJECT that periodically polls the value of the variable.
EDIT Since the FEX uitab is based on uipanel, the callback you're looking for is most likely ButtonDownFcn. Before you change it, make sure that it's not used by the uitab function, otherwise, you will want to edit that function.

Related

In Matlab, how to call GUI callback functions without the GUI?

I am not a GUI programmer, as will become obvious. If anything, I'm trying to unmake a GUI. I am using a program called art (found here, if it's useful to see) that generates figures and variables that I would like to save. You can call art from a batch script and make it read a config file for its inputs, which is what I'm doing, but you have to manually generate and save much of its output (variables and figures) in the GUI. I'd love to automate this process, but I am really struggling.
I think the core of my issue would be solved if I knew how to force the callback functions to be called. For instance, there is a function in art showCorr_Callback(hObject, eventdata, handles) (which is governed by a radio button in the GUI). It has a test condition to execute:
if (get(handles.showCorr,'Value') == get(handles.showCorr,'Max'))
I've tried inserting
mx = get(handles.showCorr,'Max'))
setappdata(handles.showCorr,'Value', mx)
into a function I know executes, the opening function, function art_OpeningFcn(hObject, eventdata, handles, varargin). That doesn't seem to have any effect. If I knew how to make the callback functions execute, perhaps I could insert the code that saves the figure into the functions. Somewhere in Matlab's GUI scripts there must be something that is continually testing for a change in state in the GUI. Where is that thing? How can I fool it into thinking a radio button has been pressed?
Thanks for your help, and please let me know if I need to provide more information.
First of all, if you want to set the Value of handles.showCorr, you won't use setappdata as that just stores arbitrary data (by key/value pair) into the graphics object. You actually want to set the Value property.
set(handles.showCorr, 'Value', get(handles.showCorr, 'Max'))
This should trigger any callbacks that are assigned to handles.showCorr.
If, for some reason this doesn't trigger the callback, you can always trigger it manually. If you already know the callback, you can call it explicitly.
showCorr_Callback(hObject, eventdata, handles);

Check if editbox is active (and evaluate WindowKeyPressFcn only if its not) in matlab

I would like to be able to test whether given edit box (uicontrol with 'style' set to 'edit') is being used at the moment (whether the cursor is inside that box / the edit box has focus).
Unfortunatelly the 'Selected' property of the editbox seems to be 'off' all the time and it's use is discouraged by MathWorks
I am open to Java solutions (but I have not tried any yet).
Background
My broader goal is to activate specific functions in response to keys pressed by the user (keyboard shortcuts) but only if the user is not typing these letters into an edit box.
I evaluate keyboard shortcuts with 'WindowKeyPressFcn' callback. I thought about using editbox's 'KeyPressFcn' callback to block 'WindowKeyPressFcn' (by passing something through setappdata or 'UserData') but it seems that 'WindowKeyPressFcn' is always evaluated first.
So now I'm trying to detect whether the editbox is in use some other way.
Since MATLAB uicontrols have underlying Java Swing GUI controls, I can think of two "Java solutions":
Associate a FocusGainedCallback with the Java object of editbox (using FindJObj - see below) (and possibly other UI elements), and update some handle \ persistent \ global variable from this function to hold the currently focused object handle. This type of callback is mentioned in an UndocumentMatlab article on uicontrol callbacks.
You should be able to use Java'sisFocusOwner() method on the underlying java object of your editbox (this method is mentioned in the Java documentation of the focus subsystem).
Note: the simplest way to get the java object is using the utility FindJObj that is briefly described here.
An alternative, much simpler method, would be to use the WindowKeyReleaseFcn callback of the GUI figure. This gets fired after the release of the keystroke, hence it gets executed after the gco gets the updated value of the currently focused uicontrol. The following check in the beginning of the WindowKeyReleaseFcn can determine if the keystroke comes from the edit box:
function figure1_WindowKeyReleaseFcn(hObject, eventdata, handles)
% gco is already updated by the time we are here to reflect the uicontrol
% which is in focus
if isequal(gco, handles.hEditBox) % Keystroke comes from the edit box
return
end
% Keystroke does not come from the edit box -- Do something here...

How to change LabVIEW Control value (in already running VI) via Active X?

I am trying set a control value in my VI (which is already running).
I use the following commands:
e=actxserver('LabVIEW.Application');
vipath='C:\DATA\Labview\test.vi';
vi=invoke(e,'GetVIReference',vipath);
% my control parameter is z which is DBL (double precision)
vi.SetControlValue('z',10)
Everything seems to work fine ! I can see the 'z' value change to 10 in VI but actually VI is not reading that value and VI application is not responding to this value
The VI is a third party application, which is developed by someone else. Unfortunately, I don't have privilege in this forum to post a picture of it (I need 10 points)
I am trying to control variable "z" from Matlab. The "z"is an input to a .dll file. Note that I can do it by front panel control in VI, but can't using Matlab as described earlier.
Without seeing the code, it's impossible to tell, but here are a couple of guesses:
Are you reading the control using a value change event? This event is only triggered either by a change in the UI or by calling the Value(Signaling) property for the control. I'm not sure if you can access this property from the ActiveX interface, but you can try by getting a reference to the control (although I have no idea how that's done from the ActiveX interface either. Maybe the VI has a method?). If you can't, the best would probably be to change the VI to poll the control. You could also have another loop which will fire the event whenever the control changes, but if you do that, I would suggest you have a separate control just for passing that value.
Another possibility - the value is read from the control's terminal before you modified it and your code uses the value on the wire, not the value from the control.
If that doesn't help, post the code.
Got it ! The control variable 'z' was within a event structure, and it was set to value change of 'z'. So, I just removed it for the time being and it works well.
However, I am not sure how to work with an event structure to control it using Matlab active x

Avoid interruption of callback functions in Matlab GUI

I have a Matlab GUI that needs a high time to execute some callback functions. Besides, these functions include the following code:
drawnow('expose');
pause(handles.data.delay);
I want to avoid that those callback executions get interrupted in order to avoid data inconsistency if the user presses other buttons. Thus, I modify the figure settings as:
set(handles.figure, 'BusyAction','cancel', 'Interruptible','off');
However, the callbacks are still interrupted. How can I avoid it?
Note: I think that the problem is that I need to propagate the 'BusyAction' and 'Interruptible' values to all the controls in my GUI, is there any way to do it automatically? Like, for example, modifying the default value before generating the GUI.
The fastest and cleanest way to propagate any property to all UI objects is with findobj:
set(findobj('Type','uicontrol'), 'BusyAction','cancel', 'Interruptible','off');

Can two panels share a uicontrol in a MATLAB GUI?

I've got a MATLAB GUI that has different aspects of functionality, each with their own panel of uicontrols. When one panel is selected, the other one is set to invisible, and vice-versa. However, they share some of the same inputs in the form of a popup menu. Can I include a 'clone' instance of the menu on the second panel somehow? I'd like to avoid as many redundant callbacks and uicontrols as possible.
I guess if the uicontrol was a direct child of the figure, you may be able to put it in front of everything.
A much simpler solution is to use the same callback for multiple uicontrols. In the property editor, you can modify the callback name and set it to a common callback function. Additionally, you can create a field (e.g. myPopupH) in the OpeningFcn of the GUI, in which you store the handles of the popups that should behave the same way. Then, in the callback, you'd use hObject, i.e. the first input argument, for all the get calls (to access the modified state of the popup-menu), but you'd use handles.myPopupH in all the set calls, so that you can ensure that both popups always have the same state. Thus, the ui-object may be redundant, but all the code (which is much more critical) only exists in a single copy.
One place where I routinely use a single callback for multiple ui elements is the close request function which is accessed from the "Cancel"-button as well as from the "X" that closes the figure, and possibly from one of the "File"-menu items.