Can't reach line object from callback function - 'Invalid or deleted object.'
Ältere Kommentare anzeigen
Hello!
Let me begin by saying that I'm not used to this part of Matlab so I apologize for any trauma that may occur. I am trying to plot a bunch of lines, and toggle their 'visible' property on/off using check boxes. What adds a bit of complexity is that my plot data is extracted from a logfile using regex, and so the number of lines to plot may vary (so I can't hard code). By reading on this forum I managed to find how to generate the checkboxes with a loop. I've also managed to plot the desired lines in the same figure. The callback function seems to work as intended, with the exception that I can't actually seem to reach the line object, and instead get the error 'Error using matlab.graphics.chart.primitive.Line/set. Invalid or deleted object.'
I've scavenged a bunch of code that I found on this forum without managing to do it. My code below is attempt #354, so if you're wondering "Why is X there?", the answer might be "giraffe". I've made up some random data so that it's executable and it seems to reproduce my issue. Thanks in advance!
clc
guiobj.f = figure;
guiobj.ax = axes;
guiobj.ax.Parent = guiobj.f;
hold on
% Mock data
Names = ['A'; 'B'];
obj{2,1} = rand(100, 4); % replicates annoying format of my logfile read
%----------
posFig = get(guiobj.f,'Position');
hFig = posFig(4); % height of the figure
for k=1:length(Names)
hold on
% Create checkboxes
guiobj.chkbox(k) = uicontrol('Parent',guiobj.f,'Style','checkbox','String',Names(k), ...
'Value',0,'Position',[30 hFig - 30 - 20*(k-1) 160 20], ...
'Callback',{@checkBoxCallback,k, guiobj});
% Create lines
guiobj.line(k) = plot(guiobj.ax, obj{2,1}(:,2*k)); % skip every other column because they contain Names (regex junk)
set(guiobj.line(k),'visible','off') % 'Visible off' at creation in loop
guidata(guiobj.f, guiobj.line(k));
end
% 'Visible on' as proof that all lines are reachable after loop.
% Try changing to off and executing the line (and back again) to see that toggle works.
set(guiobj.line(:),'visible','on')
function checkBoxCallback(source, data, k, guiobj)
fprintf('Check box %s is %d\n',get(source,'String'),get(source,'Value'));
if get(source,'Value') == 1
disp('true'); % Confirmation of entering
set(guiobj.line(k),'visible','on')
else
disp('false') % Confirmation of entering
set(guiobj.line(k),'visible','off')
end
end
%%
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Interactive Control and Callbacks finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!