编辑输入字符串类型 matlab 的问题

issue with edit input string type matlab

我收到一个错误:

Error using edit (line 31)
The input must be a string.

Error in showTF>callback_update_model (line 507)
    vars.dropheight = str2num(get(edit(2),'String'));

我的代码在这里:

params = varargin{1};

这是 2 个输入之一:

edit(2) = uicontrol('Parent',jCalc,'Units','Pixels','Position',[300 20 100 20],'String',num2str(params(2)),'Style','edit',...
            'Callback',@edit_2,...
            'BackgroundColor',[1 1 1],'ToolTipString','Puck Drop Height');

按钮回调:

function callback_update_model(~,~)
    vars.dropheight = str2num(get(edit(2),'String'));
    vars.armradius = str2num(get(edit(1),'String'));
    kiddies = get(guiel.hAX(3),'Children');
    delete(kiddies); 
    clear kiddies;
    set(guiel.tfPanel,'Visible','off','Position',cnst.tfPanelpos);
    set(guiel.hAX(1),'Position',cnst.axpos1);

    if ishandle(guiel.hAX(2)) 
    set(guiel.hAX(2),'Position',cnst.axpos2);
    end
    eval(get(guiel.hPB(4),'Callback'));
end

varargin 显示 1X1 cell [2],我很困惑如何转换输入值。

你的问题是你使用了 edit 作为变量,但它也是一个 built-in function 期望文件名作为字符串输入,因此你的错误。您应该避免像这样隐藏内置函数,因为它通常会导致问题和歧义。

通常,变量将取 precedence 并用于代替阴影函数。在这种情况下,该函数似乎是通过 2 输入调用的,而不是使用 2 索引的变量。这可能是因为 callback_update_model 函数可能无法访问 edit 变量。以下是解决此问题的方法:

  1. 将变量的名称更改为类似hEdit的名称(我通常在存储handle graphics objects的变量前加上h)。

  2. 检查 callback_update_model 是否正确 nested so that it has access to hEdit, or provide hEdit to it in another way