将参数传递给 GUIDE GUI 时发出警告

Warning when passing arguments to a GUIDE GUI

我正在尝试将参数传递给 GUIDE 生成的 GUI。这有效但会引发警告。

inputTest('Passed In String')

Warning: The input to STR2FUNC "Passed In String" is not a valid function name. This will generate an error in a future release.

我知道我过去曾向 GUIDE GUI 传递参数,但我不记得以前见过此警告消息。但是,我对 Matlab 2016b 比较陌生,所以可能发生了一些我不知道的变化。我在文档中没有找到任何内容。

警告发生在 "initialization code - DO NOT EDIT" 自动生成的部分,并且仅在传入可选参数时发生。

这是 2016b 中的错误还是我遗漏了什么?

下面的最小示例。这是一个简单的 GUI,只有一个编辑框。

function varargout = inputTest(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @inputTest_OpeningFcn, ...
                   'gui_OutputFcn',  @inputTest_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1}); % WARNING OCCURS HERE
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

function inputTest_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
% Fill the box if the string is passed in.
if nargin == 4
    handles.edit1.String = varargin{1};
else
    handles.edit1.String = 'Nothing Passed In';
end   
guidata(hObject, handles);

function varargout = inputTest_OutputFcn(hObject, eventdata, handles) 
varargout{1} = handles.output;

function edit1_Callback(hObject, eventdata, handles)
function edit1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

默认的 GUIDE GUI 行为在自动生成的内联文档中进行了解释:

% ASDF MATLAB code for asdf.fig
%      ASDF, by itself, creates a new ASDF or raises the existing
%      singleton*.
%
%      H = ASDF returns the handle to a new ASDF or the handle to
%      the existing singleton*.
%
%      ASDF('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ASDF.M with the given input arguments.
%
%      ASDF('Property','Value',...) creates a new ASDF or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before asdf_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to asdf_OpeningFcn via varargin.

您尝试使用的语法是为调用 GUI 本地的回调而保留的语法,这就是它在第一个输入上使用 str2func 调用的原因。删除 str2func 调用或更改输入语法以接受与 GUIDE 设计的输出不同的单独输出。