Not enough input arguments

1 次查看(过去 30 天)
sara syeda
sara syeda 2020-12-3
why wont the following code run and keeps giving me the not input arguments error? the three parameters are passed from a simulink model, all three are set to arrays in the toWorkspace setting.
function drawpendulum(time,theta,x)
% Check if x was passed or not
if nargin==2
% If x was not passed, just set x to a vector of zeros
x = zeros(1,length(theta));
end
% Calculate the time different between subsequent time samples
timediff=[diff(time)];
% Limit the refresh to about 10 per second
skip=ceil(0.1*length(time)/(time(end)-time(1)));
figure(1); clf; hold on;
axis([min(x)-2, max(x)+2, -1.1, 1.1]);
for i=1:skip:length(theta)-1
h1=plot(x(i)+sin(theta(i)), cos(theta(i)), 'o');
h2=line(x(i)+[0 sin(theta(i))], [0 cos(theta(i))]);
pause(timediff(i));
drawnow;
delete(h1); delete(h2);
end
end

回答(1 个)

Paul Hoffrichter
Paul Hoffrichter 2020-12-4
Cannot speak to your simulink model, but the MATLAB code below runs without error.
time = 1:1/100:1000;
theta = -pi/2:pi/(max(time)):pi/2;
x = 5*sin(theta);
drawpendulum(time,theta,x);
drawpendulum(time,theta);
You can put a check in to determine that you have exactly 2 or 3 input args.

类别

Help CenterFile Exchange 中查找有关 Debugging and Analysis 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by