callback function in gui does not know handles

2019-09-05 08:58发布

I try to implement a fall back function in my gui that updates a waitbar. I pass the function handle into the function which loads the images. The fallback itself works, but I do not get the handle to the waitbar.

function z_WaitBarUpdate(value, maxValue)
handles=guidata(handles.output); % <-- fails because handles is unknown
if ishandle(handles.waitbar.handle)
    waitbar(value/maxValue,handles.waitbar.handle,handles.waitbar.text);
end

I call the function with the handle with this code

hWait = waitbar(0,'1','Name','Reading calibration file ...');
cleanupWaitbar = onCleanup( @()( delete( hWait )));       
handles.waitbar.handle = hWait;
handles.waitbar.text = 'reading subset of stack ...';
readCalibrationImage( handles , @z_WaitBarUpdate);

any idea how to access the waitbar handle in my callback?

1条回答
在下西门庆
2楼-- · 2019-09-05 09:13

I don't typcially use most o the features you are using, I'm not a Matlab GUI guy. But, I think this would work.

  1. Change

    function z_WaitBarUpdate(value, maxValue) 
    

    to

    function z_WaitBarUpdate(value, maxValue, handles)
    
  2. Change

    readCalibrationImage( handles , @z_WaitBarUpdate); 
    

    to

    readCalibrationImage( handles , @(value, maxValue) z_WaitBarUpdate(value, maxValue, handles));
    

First, this defines a third input to the update function, to handle the input that you are missing. If handles is not passed in then it will not be available to the function. Then, after the handles structure has been created, this creates an anonymous function with the purpose of defining the third input, while allowing the first two inputs to be defined later, when the function is actually called.

查看更多
登录 后发表回答