Uicontrol callback stops working in new session

1 次查看(过去 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?

回答(1 个)

Vishnu priya v
Vishnu priya v 2022-9-19
In the Start-up Function, you can write
label_visibility(hObject);
you can try doing this.

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

产品


版本

R2013a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by