Uicontrol callback stops working in new session
3 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Vishnu priya v
on 19 Sep 2022
In the Start-up Function, you can write
label_visibility(hObject);
you can try doing this.
0 Comments
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!