在多个 Matlab 绘图调用期间连接点
connect points during multiple Matlab plot calls
我想在多个 Matlab 绘图调用期间连接两个或多个数据点。下面是一些虚拟代码,显示了我要完成的任务,
function main()
close all
t = 0;
while t<10
x = rand(1,1);
y = rand(1,1);
t = t+1;
calculateErrorAndPlot(1,x,y,t)
end
return
function calculateErrorAndPlot(figureNumber,x,y,time)
figure(figureNumber);
if ~ishold
hold on
end
error = x-y;
plot(time,error,'.b');
return
现在,我必须使用“.b”至少可以看到绘制的数据点。请注意,绘图是使用标量调用的。
我会重写这个,并删除函数:
time = [1:10];
for ii = 1:length(time)
x = rand(1,1);
y = rand(1,1);
error(ii) = x-y;
end
plot(time,error,'.-b')
您可以更新已绘制线条的 XData and YData 属性以添加新点。例如:
function main
fig = figure;
ax = axes( 'Parent', fig );
line = plot( NaN, NaN, 'Parent', ax );
for t = 0:9
x = rand( 1, 1 );
y = rand( 1, 1 );
calculateErrorAndPlot( line, x, y, t )
end
end
function calculateErrorAndPlot( line, x, y, t )
xData = get( line, 'XData' );
yData = get( line, 'YData' );
xData(end+1) = t;
yData(end+1) = x - y;
set( line, 'XData', xData, 'YData', yData );
end
这是一些有效的代码。但是,我愿意接受建议或其他可能更简单 and/or 更快 :)
的代码
function calculateErrorAndPlot(figureNumber,x,y,time)
figure(figureNumber);
if ~ishold
hold on
end
error = x-y;
h = findobj(figure(figureNumber),'Type','line');
if isempty(h)
xx = time;
yy = error;
else
xx = [h(1).XData(end) time];
yy = [h(1).YData(end) error];
end
plot(xx,yy,'.-black');
return
我想在多个 Matlab 绘图调用期间连接两个或多个数据点。下面是一些虚拟代码,显示了我要完成的任务,
function main()
close all
t = 0;
while t<10
x = rand(1,1);
y = rand(1,1);
t = t+1;
calculateErrorAndPlot(1,x,y,t)
end
return
function calculateErrorAndPlot(figureNumber,x,y,time)
figure(figureNumber);
if ~ishold
hold on
end
error = x-y;
plot(time,error,'.b');
return
现在,我必须使用“.b”至少可以看到绘制的数据点。请注意,绘图是使用标量调用的。
我会重写这个,并删除函数:
time = [1:10];
for ii = 1:length(time)
x = rand(1,1);
y = rand(1,1);
error(ii) = x-y;
end
plot(time,error,'.-b')
您可以更新已绘制线条的 XData and YData 属性以添加新点。例如:
function main
fig = figure;
ax = axes( 'Parent', fig );
line = plot( NaN, NaN, 'Parent', ax );
for t = 0:9
x = rand( 1, 1 );
y = rand( 1, 1 );
calculateErrorAndPlot( line, x, y, t )
end
end
function calculateErrorAndPlot( line, x, y, t )
xData = get( line, 'XData' );
yData = get( line, 'YData' );
xData(end+1) = t;
yData(end+1) = x - y;
set( line, 'XData', xData, 'YData', yData );
end
这是一些有效的代码。但是,我愿意接受建议或其他可能更简单 and/or 更快 :)
的代码function calculateErrorAndPlot(figureNumber,x,y,time)
figure(figureNumber);
if ~ishold
hold on
end
error = x-y;
h = findobj(figure(figureNumber),'Type','line');
if isempty(h)
xx = time;
yy = error;
else
xx = [h(1).XData(end) time];
yy = [h(1).YData(end) error];
end
plot(xx,yy,'.-black');
return