What is the syntax for using multiple inputs for a callback function within a gui set command?

For instance, I might use the following set command in the base workspace of some gui application:
set([someuiname.sl,someuiname.ed,someuiname.rb],'callback',{@somefunction,someuiname});
As I understand how this works, every time the gui slider, edit box, or radio button is changed the set command calls the function "somefunction" to execute. The input to "somefunction" is the entire gui structure "someuiname", and upon execution the entire gui gets updated. My question concerns the possibility of having multiple inputs into "somefunction." Suppose "somefunction" requires additional inputs, i.e. one or more structures (independent of the gui structure "someuiname"), arrays, or other data for execution. This could be the case, for example with a simulation, when a data structure needs to be updated along with the gui everytime a gui element is changed. I might think the following syntax could work for this purpose:
set([someuiname.sl,someuiname.ed,someuiname.rb],'callback',{@somefunction,someuiname,...someway to list additional data inputs?});
Maybe pointers to the additional data inputs need to be setup and incorporated into the set command? If so, how?

 Akzeptierte Antwort

Although you can always resort to global variables or using evalin() and assignin(), to directly manipulate any data in a workspace, without actual need to pass and return. But these practices and highly unrecommended and only use for small projects when you actually know what you are trying to do.
For a proper solution, here is one way you can achieve what you are trying to do. You actually want to pass the extra input variables by reference. In MATLAB handle classes exhibit this behavior. All variables to a handle object will point to the same memory location and if one of them is modified, every other will show the change. You need to define your own handle class, with data properties as you want. Then create variables of your class and pass them to callback functions as input. When they change inside the callback, the actual copy will change too.

4 Kommentare

Another way I've tried is to save/load the data to/from the disk whenever needed, but this is clunky.
As far as creating a handle class with data properties, I'm not clear on this. I tried to define/initialize the simulation data by appending the structure that is used for the gui object values...like, someuiname.dat (where someuiname.dat is the data variable that is supposed to get updated whenever the gui is updated...someuiname.dat could be an N x M x L matrix of numbers). But someuiname.dat as initiated in the base workspace doesn't seem to get updated by the set command. I need to see a code example.
@phillip Here is a simple example to give you a general layout.
  • First Define a handle class. Here is a simple handle class named MyClass with just one property value and a constructor.
classdef MyClass < handle
properties
value
end
methods
function obj = MyClass(in)
obj.value = in;
end
end
end
save this class in a .m file in MATLAB path with name MyClass.m. In order to see the difference between handle class and a normal class, try the following lines.
x = MyClass(10); % initialize a variable of type MyClass and initialize it with 10
y = x; % assign x to y. But since x is handle class, only address is shared.
y.value = 15;
disp(x.value);
you will see that by changing the value in y, x is also changed.
  • Create a GUI. Here am creating a simple GUI with only one pushbutton.
f = figure;
button = uicontrol(f, 'String', 'Button')
  • Define a callback function, in which add an extra argument for your data objects. For example
function callbackFun(HObj, event, myData)
myData.value = myData.value + 1;
end
save this callback function in a file on MATLAB path.
  • Attach this callback with ButtonDownFcn of the pushbutton, add extra elements using the curly bracket notation ( x is same as initialized earlier) as given below
button.ButtonDownFcn = {@callbackFun, x}
  • Now right-click on the push-button one. In the base workspace. check the value in x. It will be increased by 1, as specified in the callback function.
You can use this method to handle more complicated datatypes.
Thank you for this example.....opens a few doors of understanding (i.e. use of classdef).

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Function Creation finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by