One function for multiple pushbuttons in matlab -
i thinking of setting 1 function multiple pushbuttons, same thing, has different defining value. when 1 pushbutton activated not mixed other pushbutton of same function
see the documentation callbacks. callbacks accept 2 input arguments default: handle of object invoked function , structure of event data object, may or may not empty. can use string or tag properties of pushbutton control behavior of gui based on button pressed using single callback function. consider following example:
function testgui handles.mainwindow = figure(); handles.mytextbox = uicontrol( ... 'style', 'edit', ... 'units', 'normalized', ... 'position', [0.15 0.80 .70 .10], ... 'string', 'no button has been pressed' ... ); handles.button(1) = uicontrol( ... 'style', 'pushbutton', ... 'units', 'normalized', ... 'position', [0.05 0.05 .30 .70], ... 'string', 'button1', ... 'callback', {@mybuttonpress,handles} ... ); handles.button(2) = uicontrol( ... 'style', 'pushbutton', ... 'units', 'normalized', ... 'position', [0.35 0.05 .30 .70], ... 'string', 'button2', ... 'callback', {@mybuttonpress,handles} ... ); handles.button(3) = uicontrol( ... 'style', 'pushbutton', ... 'units', 'normalized', ... 'position', [0.65 0.05 .30 .70], ... 'string', 'button3', ... 'callback', {@mybuttonpress,handles} ... ); end function mybuttonpress(src, ~, handles) switch src.string case 'button1' handles.mytextbox.string = 'button 1 has been pressed'; case 'button2' handles.mytextbox.string = 'button 2 has been pressed'; case 'button3' handles.mytextbox.string = 'button 3 has been pressed'; otherwise % strange happened end end note requires matlab r2014b or newer in order use dot notation accessing object properties. see this blog post more information.
Comments
Post a Comment