draggable scatter plot for pole location

조회 수: 7 (최근 30일)
Guillaume Ryelandt
Guillaume Ryelandt 2023년 6월 21일
편집: Sai Teja G 2023년 10월 10일
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
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.
Guillaume Ryelandt
Guillaume Ryelandt 2023년 6월 21일
Thank you for your answer, actually i do not get an error while running the code but when i try to drag the poles on the pole location plot, i get the error, is the case for you as well.
Cheers,
Guillaume.

댓글을 달려면 로그인하십시오.

답변 (1개)

Sai Teja G
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');

카테고리

Help CenterFile Exchange에서 Plot Customization에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by