找到你要的答案

Q:Matlab Too many output arguments from GUI figure declaration

Q:MATLAB的GUI图形输出变量过多的宣言

I have a GUI that I want to catch errors for and handle with a function I have called generateReport. The first GUI file (there are multiple windows each with it's own programmatic GUI file) is CSTMainWindow and the Program is run from the Celest function.

CeleST:

function CeleST

c(@CSTMainWindow); % line 3

end

c.m is just a wrapper for functions so I can try/catch them so I don't have to write code in every subfunction

function c(fxn)
% Function to wrap callback functions with try/catch and call
% generateReport.m in case of error

try
    fxn(); % line 6
catch exception
    generateReport(exception)
end

end

So CSTMainWindow is called but then it breaks on this line (the first GUI code line really) CSTMainWindow.m line 142:

mainFigure = figure('Visible','off','Position',[5,40,mainW,mainH],'Name','CeleST: Check results','numbertitle','off', 'menubar', 'none', 'resizefcn', c(@resizeMainFigure));

When debugging, c.m catches the error:
identifier: 'MATLAB:TooManyOutputs'
message: 'Too many output arguments.'
stack:
line name
142 CSTMainWindow
6 c
3 CeleST

In trying to resolve this I found that changing the callback to just resizeMainFigure fixes it which makes sense, but then to get the try/catch behavior I want, I would have to but a block everywhere which I was doing before and I'm trying to avoid.

My question is why am I getting too many output arguments if CSTMainWindow has zero output arguments and neither does resizeMainFigure (below for reference)

function resizeMainFigure(hObject,eventdata) %#ok<INUSD>
        % -------
        % Update the size and position of the sliders
        % -------
        newPosition = get(mainFigure,'position');
        set(sliderHoriz, 'position',[0 0 newPosition(3)-20 20]);
        set(sliderVert, 'position',[newPosition(3)-20 20 20 newPosition(4)-20]);
        % -------
        % Check the horizontal slider
        % -------
        if newPosition(3) < mainPanelPosition(3)
            deltaH = round(mainPanelPosition(3) - newPosition(3));
            newValue = min(deltaH,get(sliderHoriz,'value'));
            set(sliderHoriz, 'enable', 'on', 'min',0,'max',deltaH,'value',newValue);
        else
            set(sliderHoriz, 'enable', 'off','min',0,'max',1,'value',0);
        end
        % -------
        % Check the vertical slider
        % -------
        if newPosition(4) < mainPanelPosition(4)
            deltaV = round(mainPanelPosition(4) - newPosition(4));
            newValue = min(deltaV,get(sliderVert,'value'));
            set(sliderVert, 'enable', 'on', 'min',0,'max',deltaV,'value',newValue);
        else
            set(sliderVert, 'enable', 'off','min',0,'max',1,'value',0);
        end
        setMainPanelPositionBySliders
end

我有一个图形用户界面,我想抓住的错误处理功能,我称之为generatereport。第一个图形文件(有多个窗口,每个都有它自己的编程GUI文件)是CSTMainWindow和程序运行时的功能。

CeleST:

function CeleST

c(@CSTMainWindow); % line 3

end

新仅仅是一个包装功能,所以我可以尝试/赶上他们,所以我不需要在每个子函数编写代码

function c(fxn)
% Function to wrap callback functions with try/catch and call
% generateReport.m in case of error

try
    fxn(); % line 6
catch exception
    generateReport(exception)
end

end

So CSTMainWindow is called but then it breaks on this line (the first GUI code line really) CSTMainWindow.m line 142:

mainFigure = figure('Visible','off','Position',[5,40,mainW,mainH],'Name','CeleST: Check results','numbertitle','off', 'menubar', 'none', 'resizefcn', c(@resizeMainFigure));

When debugging, c.m catches the error:
identifier: 'MATLAB:TooManyOutputs'
message: 'Too many output arguments.'
stack:
line name
142 CSTMainWindow
6 c
3 CeleST

在试图解决这个我发现改变回调只是resizemainfigure修复它是有道理的,但后来把try/catch行为我要,我要一块到处都是,但我在做之前,我试图避免。

我的问题是为什么我得到了太多的输出参数如果CSTMainWindow零输出参数也没有resizemainfigure(参考下面)

function resizeMainFigure(hObject,eventdata) %#ok<INUSD>
        % -------
        % Update the size and position of the sliders
        % -------
        newPosition = get(mainFigure,'position');
        set(sliderHoriz, 'position',[0 0 newPosition(3)-20 20]);
        set(sliderVert, 'position',[newPosition(3)-20 20 20 newPosition(4)-20]);
        % -------
        % Check the horizontal slider
        % -------
        if newPosition(3) < mainPanelPosition(3)
            deltaH = round(mainPanelPosition(3) - newPosition(3));
            newValue = min(deltaH,get(sliderHoriz,'value'));
            set(sliderHoriz, 'enable', 'on', 'min',0,'max',deltaH,'value',newValue);
        else
            set(sliderHoriz, 'enable', 'off','min',0,'max',1,'value',0);
        end
        % -------
        % Check the vertical slider
        % -------
        if newPosition(4) < mainPanelPosition(4)
            deltaV = round(mainPanelPosition(4) - newPosition(4));
            newValue = min(deltaV,get(sliderVert,'value'));
            set(sliderVert, 'enable', 'on', 'min',0,'max',deltaV,'value',newValue);
        else
            set(sliderVert, 'enable', 'off','min',0,'max',1,'value',0);
        end
        setMainPanelPositionBySliders
end
answer1: 回答1:

Check the last argument to figure that you have. It's a function call to c, which returns nothing, but you're passing it as a parameter to the figure call. c has no return value but, in effect, you're asking for it to return a value for you. Based on the figure documentation, you have to provide a function handle, a cell array containing a function handle, or a string that is a valid MATLAB expression for resizefcn. You might try turning your function call into a string instead:

mainFigure = figure('Visible','off','Position',[5,40,mainW,mainH],'Name','CeleST: Check results','numbertitle','off', 'menubar', 'none', 'resizefcn', 'c(@resizeMainFigure)');

检查最后一个参数以确定你有。这是对C的函数调用,它不返回任何内容,但你将它作为参数调用。C没有返回值,但实际上,你要求它返回一个值给你。基于图的文件,你必须提供一个处理函数,包含功能处理单元阵列,或一个字符串,是一个有效的resizefcn MATLAB表达式。您可以尝试将函数调用转换为字符串:

mainFigure = figure('Visible','off','Position',[5,40,mainW,mainH],'Name','CeleST: Check results','numbertitle','off', 'menubar', 'none', 'resizefcn', 'c(@resizeMainFigure)');
matlab  callback  try-catch