I'm trying to programmatically create a click event in MATLAB that will mimic the user clicking on a GUI object. The callback function for the object is a subfunction, so I can't call it directly. However, I am able to get the callback property from the object, which ends up being a 3-by-1 cell array with the following contents:
#uiBlockFn/callback_til [ 188.0011] [1x1 struct]
How can I invoke this callback function in code such that it mimics what would happen when a user clicks the GUI object?
Let's say you have a graphics object with handle hObject, and you got the callback for the object like so:
callbackCell = get(hObject,'Callback');
As you mentioned, the cell array callbackCell that you get ends up being a 3 element cell array with a function handle in the first cell and other data in the other two cells. When the callback for an object is defined as a cell array (like it is in your case), the callback function handle (or string name) is stored in the first cell and additional input arguments you want passed to the callback function are in the remaining cells.
However, when this callback is invoked when the object is activated, there will actually be 2 additional arguments automatically inserted by MATLAB at the beginning of the input argument list. These are:
hObject: The handle to the object whose callback is now being called.
eventData: A structure of data related to the user-activated event, which is often just the empty matrix [] (except in a few cases).
So, if you want to mimic the action of the object being activated by the user, you would want to invoke your callback function as follows (assuming there is no event data needed):
callbackCell{1}(hObject,[],callbackCell{2:end});
This is what the built-in hgfeval function is for:
http://undocumentedmatlab.com/blog/hgfeval/
Related
I have a scenario where I have to validate whether one of the input fields have values entered.
In the OnBlur Event of each input field I get the value and set in the state, and in the mean time I check whether there is values in one of those input boxes and set the inputIsReq state. And I use that inputIsReq in the required attribute in each text field. After this change my page loads really slow, and the validations doesn't happen too. Any idea to fix this ?
Your code is running slow because you are calling the method changeInputValue instead of passing it as a callback.
this will call changeInputValue:
wrong:
onBlur={this.changeInputValue("input_4_value", this)}
correct:
onBlur={() => this.changeInputValue("input_4_value", this)}
Abowe example will pass changeInputValue as a callback.
you can also
onBlur={changeInputValue} but then you can't pass any params to callback
You were calling changeInputValue in a loop since it have changed the state and then component was re rendered and so on... This was also producing the warning in a console
warning.js:36 Warning: setState(...): Cannot update during an existing
state transition (such as within render or another component's
constructor). Render methods should be a pure function of props and
state; constructor side-effects are an anti-pattern, but can be moved
to componentWillMount.
I just stepped on this bug (or maybe it's a "feature")
If I pass a bound method (defined with fat arrow) as a D3js callback, it gets called with null arguments:
d3.json "some.json", #handler
If I enclose it in a bound closure (fat arrow) then it receives its arguments ok:
d3.json "some.json", (json) => #handler json
Here is a JSFiddle showing the issue.
Passing fat-arrow bound methods as standard Javascript callbacks (eg. to setTimeout) or as jQuery callbacks or event handlers is a proven technique, in fact I have an entire application written this way.
Why is it not working in D3js?
The issue is that the callback for d3.json has two arguments (see the documentation):
[T]he callback is invoked with two arguments: the error, if any, and the parsed JSON.
If D3 detects that the callback function has only one argument, it only passes the data, but it can't do that in your first example. Changing the definition of the handler function accordingly fixes the issue:
directHandler: (e, x) =>
#log "directHandler received #{JSON.stringify x}"
Complete demo here.
I'm trying to programmatically create a click event in MATLAB that will mimic the user clicking on a GUI object. The callback function for the object is a subfunction, so I can't call it directly. However, I am able to get the callback property from the object, which ends up being a 3-by-1 cell array with the following contents:
#uiBlockFn/callback_til [ 188.0011] [1x1 struct]
How can I invoke this callback function in code such that it mimics what would happen when a user clicks the GUI object?
Let's say you have a graphics object with handle hObject, and you got the callback for the object like so:
callbackCell = get(hObject,'Callback');
As you mentioned, the cell array callbackCell that you get ends up being a 3 element cell array with a function handle in the first cell and other data in the other two cells. When the callback for an object is defined as a cell array (like it is in your case), the callback function handle (or string name) is stored in the first cell and additional input arguments you want passed to the callback function are in the remaining cells.
However, when this callback is invoked when the object is activated, there will actually be 2 additional arguments automatically inserted by MATLAB at the beginning of the input argument list. These are:
hObject: The handle to the object whose callback is now being called.
eventData: A structure of data related to the user-activated event, which is often just the empty matrix [] (except in a few cases).
So, if you want to mimic the action of the object being activated by the user, you would want to invoke your callback function as follows (assuming there is no event data needed):
callbackCell{1}(hObject,[],callbackCell{2:end});
This is what the built-in hgfeval function is for:
http://undocumentedmatlab.com/blog/hgfeval/
How can I put a new event at the top of all attached events linked to an object just after the page load?
Pratically I have an accordion using some YUI funcs to activate its behavior, now without modifying the YUI main function I need some new functions used to call AJAX routines.
Currently I am trying with Event.getListeners, but I don't know how to treat the returned objects.
Regards
The responses you get from Event.getListeners is all the arguments passed into the Event.addListener. According to the docs for Event.getListeners it returns
the listener. Contains the following fields:
type: (string) the type of event
fn: (function) the callback supplied to addListener
obj: (object) the custom object supplied to addListener
adjust: (boolean|object) whether or not to adjust the default context
scope: (boolean) the derived context based on the adjust parameter
index: (int) its position in the Event util listener cache
So using that object you can probably just remove the Listeners using Event.removeListener & then re-add them all in the order you want using Event.addListener. The object's properties map 1:1 with the args for Event.addListener so nothing will be lost.
Is it possible using NSArrayController to bind a NSTextField's value to a particular item in the array? In particular, I want to bind to a property on the first item in the array, and show nothing if the array is empty.
Using arrangedObjects.command shows just "(" -- presumably it's trying to show a multi-line string with comma-separated strings for each item. I just want the first one.
Bind the text field to selection.command, and programmatically set the array controller's selection index to 0. You may need to also re-set the selection index to 0 any time you add or remove items to the content array.
Obviously, this won't work if you're allowing the user to select items within the array controller (you'd need a second array controller). I'm assuming that's not the case, since if it were, I'd expect you to want to show the user-selected object, instead of always the first object.
EDIT: Better yet, do nothing like this—if the object that is first in the array has some special status, you should make a separate non-array property (in the same object that holds the original array, from which I assume the array controller is getting it) to hold the object that has that status.