Uicontrol callback stops working in new session
3 次查看(过去 30 天)
显示 更早的评论
I'm using the 2013a version of MATLAB to programmatically add uicontrols (textbox and togglebutton) to figures.The togglebutton is intended to toggle text labels that have been added to data points that appear to be outliers in the plot (see attached figure). If I close the figure in the same session as it was created in and re-open the figure the callback function still works. But when I close the matlab session and re-open the figure, I receive this error message after clicking the togglebutton :
Undefined function handle.
Error while evaluating uicontrol Callback
This is the code for setting up the uicontrols:
% Create textbox object with the formatted bad channel list and adjust background color to match figure background color.
textbox = uicontrol('Style', 'text','Units', 'pixels', 'Position',[figOuterPos(3)-60 figOuterPos(4)-530 90 400], ...
'String', bad_chan_list_formatted, 'BackgroundColor', get(figHandle,'Color'));
figPos = get(figHandle, 'Position');
% Adjust figure position in the y-direction by 5 pixels
set(figHandle, 'Position', [figPos(1) figPos(2) figPos(3) figPos(4)-5]);
handles = guihandles(figHandle);
textLabelObjs = findobj('Tag', 'BadChanLabel'); % Get all text label objects from figure through a tag
% Add toggle button above the bad channel list in figure to hide or show labels.
label_btn = uicontrol('Style', 'togglebutton','String', 'Toggle Labels',...
'Units','pixels','Position',[figOuterPos(3)-55 figOuterPos(4)-120 90 25], ...
'Callback',{@label_visibility},'UserData',textLabelObjs);
And this is the code for my callback function:
function label_visibility(hObject, eventData) %#ok<INUSL>
textObj = get(hObject, 'UserData');
value = get(hObject,'Value');
if value == 1
set(textObj,'Visible','off'); % Hide text labels
else
set(textObj,'Visible','on'); % Show text labels
end
end
How do I get my callback function to work everytime in new matlab sessions?
Is there a way to initialize the callback function when opening the figure?
0 个评论
回答(1 个)
Vishnu priya v
2022-9-19
In the Start-up Function, you can write
label_visibility(hObject);
you can try doing this.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!