Matlab GUI callback troubles

和自甴很熟 提交于 2019-12-04 07:14:44

问题


I try to make the uipanel change boarder colors while pressing and releasing mouse button on elsewhere except inputs and panel buttons.

function    [oldpropvalues,varargout]=DisableFigure(handlearray,prop,propvalue,varargin);

  oldpropvalues=get(handlearray,prop);

  %this IF is used to highlight the "modal" panel when anywhere outside it is pressed

  if length(varargin)==2  

    %these two are the old windowbutton functions which will be put back when the window is put back to normal.
    varargout{1}=get(varargin{1},'windowbuttondownfcn');    
    varargout{2}=get(varargin{1},'windowbuttonupfcn');

    set(varargin{1},'windowbuttondownfcn',['set(varargin{2},''bordertype'',''line'',''borderwidth'',2,''highlightcolor'',[0 0 0])']);
    set(varargin{1},'windowbuttonupfcn',['set(varargin{2},''bordertype'',''beveledout'',''borderwidth'',1,''highlightcolor'',[1 1 1])']);

  end 

  set(handlearray,prop,propvalue);

The error shows Undefined variable "varargin" or class "varargin".

Error while evaluating Figure WindowButtonDownFcn

Undefined variable "varargin" or class "varargin".

Error while evaluating Figure WindowButtonUpFcn


回答1:


Your problem is that you're defining your window callbacks as character vectors, which are evaluated in the base workspace where the variable varargin doesn't exist. You can define them as anonymous functions instead:

set(varargin{1}, 'WindowButtonDownFcn', ...
    @(~, ~) set(varargin{2}, 'BorderType', 'line', 'BorderWidth', 2, ...
                'HighlightColor', [0 0 0]));
set(varargin{1}, 'WindowButtonUpFcn', ...
    @(~, ~) set(varargin{2}, 'BorderType', 'beveledout', 'BorderWidth', 1, ...
                'HighlightColor', [1 1 1]));



回答2:


You did not show how you produced the error, but from the error message, I guess you called the function with less than 4 input arguments. Then varargin does not exist, so matlab gives the error.

To avoid the error, you need to check nargin before you use varargin, for example, replace your if statement line with

if nargin==5 % so length(varargin)==2


来源:https://stackoverflow.com/questions/46248004/matlab-gui-callback-troubles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!