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

时间:2017-12-08 01:16:41

标签: matlab matlab-guide

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

inputTest('Passed In String')
  

警告:输入STR2FUNC"传入字符串"不是有效的   功能名称。这将在将来的版本中生成错误。

我知道我过去已经通过GUIDE GUI的参数,我不记得以前曾经看过这个警告信息。但是,我对Matlab 2016b相对较新,所以有些东西可能已经改变,我不知道。我在文档中找不到任何内容。

警告发生在"初始化代码中 - 请勿编辑"自动生成的部分,仅在传递可选参数时发生。

这是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

1 个答案:

答案 0 :(得分:1)

自动生成的内联文档中说明了默认的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设计的输出不同的输出。

相关问题