Matlab Plot that changes with time

落花浮王杯 提交于 2020-01-07 06:18:09

问题


I am trying to plot a figure that changes with time (think of it as plotting the shape of a pole as the wind passes through it, so I want to plot the shape at every second).

To avoid the x axis limit changing frequently I want to fix it to the limits (max and min that I calculate before plotting). Here is a sample of my code:

for i=1:1:numberofrows
    momentvaluesatinstant = momentvalues(i,:);
    figure(1)
    plot(momentvaluesatinstant,momentheader)
    drawnow
    title(sprintf('Moment profile along pile at time 0.2%f',time(i)'))
    xlabel('Moment (kN.m)')
    xlim([momentvalues(rowminmoment) momentvalues(rowmaxmoment)])
    ylabel('Length of pile (m)')
    delay(1);
end

Although I am specifying the limits of the x axis to be fixed to the values I specify, the plot keeps changing the limits depending on the data being plotted? Is there something I am missing?


回答1:


Figured it, need to add xlim manual




回答2:


I'm not sure why you needed xlim manual, but here is a more compact and correct way to animate your data:

% use 'figure', `plot` and all the constant parts of the figure only once, before the loop.
figure(1)
m = plot(momentvalues(1,:),momentheader); % plotting only step 1
xlim([momentvalues(rowminmoment) momentvalues(rowmaxmoment)])
xlabel('Moment (kN.m)')
ylabel('Length of pile (m)')

% loop from step 2 ahead
for k = 2:length(momentvalues)
    pause(1); % use pause to set the delay between shots
    % use 'set' to change the x values
    set(m,'Xdata',momentvalues(k,:));    
    drawnow
    title(sprintf('Moment profile along pile at time 0.2%f',k))
end


来源:https://stackoverflow.com/questions/39002746/matlab-plot-that-changes-with-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!