matlabmatlab-gui

issue with edit input string type matlab


I got an error:

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'));

My code here:

params = varargin{1};

this is one of the 2 inputs:

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');

button callback:

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

the varargin shows 1X1 cell [2] which I am confused how I am converting the input values.


Solution

  • Your problem is that you have used edit as a variable, but it is also a built-in function that expects a file name as a string input, hence your error. You should avoid shadowing built-in functions like this since it generally leads to problems and ambiguities.

    Usually, the variable will take precedence and be used instead of the shadowed function. In this case, the function appears to be getting called with an input of 2 instead of the variable being indexed with 2. This may be because the callback_update_model function may not have access to the edit variable. Here's what you should do to fix this:

    1. Change the name of the variable to something like hEdit (I usually preface variables that store handle graphics objects with h).

    2. Check that callback_update_model is properly nested so that it has access to hEdit, or provide hEdit to it in another way.