draggable scatter plot for pole location
3 次查看(过去 30 天)
显示 更早的评论
I am trying to have an animated plot for pole location. My goal is to be able to drag the poles on the plot and see how the step response changes accordingly. The error I get is
Error using matlab.graphics.chart.primitive.Scatter/get
Invalid or deleted object.
Error in simu_second_order>updateSystem/dragPole (line 82)
poles = complex(get(poleHandle, 'XData'), get(poleHandle, 'YData'));
Error while evaluating Figure WindowButtonMotionFcn.
Could someone help me with that.
Thank you :)
close all
% Create the figure and axes for pole locations
poleFig = figure('Name', 'Pole Locations');
ax1 = axes(poleFig);
hold(ax1, 'on');
% Create the figure and axes for the step response
stepFig = figure('Name', 'Unit Step Response');
ax2 = axes(stepFig);
% Define the transfer function parameters
omega_0 = 1;
dzeta = 0.7;
% Create sliders for omega and dzeta
omegaSlider = uislider('Position', [100, 350, 300, 3], 'Limits', [0.1, 5], 'Value', omega_0);
dzetaSlider = uislider('Position', [100, 300, 300, 3], 'Limits', [0, 1], 'Value', dzeta);
% Create callback function for sliders
sliderCallback = @(src, event) updateSystem(omegaSlider.Value, dzetaSlider.Value, ax1, ax2);
% Set callback functions for sliders
addlistener(omegaSlider, 'ValueChanged', sliderCallback);
addlistener(dzetaSlider, 'ValueChanged', sliderCallback);
% Create an empty handle for the poles
poleHandle = [];
% Initial system update
updateSystem(omega_0, dzeta, ax1, ax2);
% Function to update the system response
function updateSystem(omega_0, dzeta, ax1, ax2)
% Calculate the coefficients of the transfer function
a = omega_0^2;
b = 2*dzeta*omega_0;
c = omega_0^2;
% Create the transfer function
sys = tf(a, [1, b, c]);
% Calculate the step response
t = 0:0.01:10; % Time vector
stepResp = step(sys, t); % Calculate the step response
% Update the pole locations plot
cla(ax1);
poles = roots([1, b, c]);
hold(ax1, 'on');
% Plot the pole locations as draggable points
poleHandle = scatter(ax1, real(poles), imag(poles), 'rx', 'LineWidth', 2);
set(poleHandle, 'ButtonDownFcn', @startDrag);
xlim(ax1, [-5 5]);
ylim(ax1, [-5 5]);
grid(ax1, 'on');
xlabel(ax1, 'Real');
ylabel(ax1, 'Imaginary');
title(ax1, 'Pole Locations');
% Update the step response plot
cla(ax2);
plot(ax2, t, stepResp, 'LineWidth', 2);
grid(ax2, 'on');
xlabel(ax2, 'Time');
ylabel(ax2, 'Step Response');
title(ax2, 'Unit Step Response');
% Function to start dragging the pole location
function startDrag(~, ~)
set(gcf, 'WindowButtonMotionFcn', @dragPole, 'WindowButtonUpFcn', @stopDrag);
end
% Function to drag the pole location
function dragPole(~, ~)
currentPoint = get(ax1, 'CurrentPoint');
x = currentPoint(1, 1);
y = currentPoint(1, 2);
% Update the selected pole location
poles = complex(get(poleHandle, 'XData'), get(poleHandle, 'YData'));
[~, index] = min(abs(poles - (x + 1i * y)));
poles(index) = x + 1i * y;
% Update the pole locations plot
set(poleHandle, 'XData', real(poles), 'YData', imag(poles));
% Update the system response
updateSystem(omega_0, dzeta, ax1, ax2);
end
% Function to stop dragging the pole location
function stopDrag(~, ~)
set(gcf, 'WindowButtonMotionFcn', '', 'WindowButtonUpFcn', '');
end
end
2 个评论
Vishnu
2023-6-21
I'm using R2023a version and I didn't get any error while running the code. Please check with the latest version.
回答(1 个)
Sai Teja G
2023-8-14
编辑:Sai Teja G
2023-10-10
Hi Guillaume,
I see that you are getting an error saying that invalid or deleted object when you tried to drag the plot in pole location figure.
The problem arises because you are using the "cla" command for the axis "ax1". When you use the "cla" command, it deletes all the graphic objects associated with that axis. As a result, when you attempted to drag the scatter plot, you encountered an error indicating that the object was either invalid or deleted. To resolve this issue, simply remove the "cla" commands from your code.
Hope this helps!
cla(ax1);% This is the reason for error, just remove this line to resolve the issue
poles = roots([1, b, c]);
hold(ax1, 'on');
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Stability Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!