我想在多个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'来至少看到正在绘制的数据点。请注意,正在使用标量调用绘图。
答案 0 :(得分:1)
您可以更新已绘制的行的XData和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
答案 1 :(得分:0)
我会重写这个,并删除该功能:
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')
答案 2 :(得分:-1)
以下是一些有效的代码。但是,我愿意接受可能更简单和/或更快的建议或其他代码:)
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