user interface - MATLAB GUI - Button Press returning error -
i have simple matlab gui code, find attached. when button pressed runs function.
however when press button twice, throwing error
undefined function 'gui' input arguments of type 'struct'.
error in @(hobject,eventdata)gui('pushbutton1_callback',hobject,eventdata,guidata(hobject))
error while evaluating uicontrol callback
% --- executes on button press in pushbutton1. function pushbutton1_callback(hobject, eventdata, handles) % hobject handle pushbutton1 (see gcbo) % eventdata reserved - defined in future version of matlab % handles structure handles , user data (see guidata) set(handles.pushbutton1, 'enable','off'); output = randomfunction(); = 1 while(1) = a+1 if == 4 break; end end set(handles.pushbutton1, 'enable','on');
the issue randomfunction
must either change current working directory or modify path
such gui function (gui.m
) no longer on path , able found when click button second time.
if you'd stop behavior have 2 options
the preferred option modify
randomfunction
not make modification. function should user's environment way before being called. can usingoncleanup
withinrandomfunction
function randomfunction() folder = pwd; cleanup = oncleanup(@()cd(folder)); % normal contents of randomfunction end
the other option within
randomfunction
though never usecd
. best practice. can use full file paths instead access filesfilename = fullfile(folder, 'image.png'); imread(filename)
if can't modify
randomfunction
can modify callback remember current directory before calling function , change afterrandomfunction
completes. recommend usingoncleanup
ensure directory changed ifrandomfunction
errors outfunction pushbutton1_callback(hobject, eventdata, handles) set(handles.pushbutton1, 'enable', 'off'); % make sure when function ends change current folder folder = pwd; cleanup = oncleanup(@()cd(folder)); output = randomfunction(); = 1 while(1) = a+1 if == 4 break; end end set(handles.pushbutton1, 'enable','on');
Comments
Post a Comment