如何在单位时间间隔内从for循环中保存和绘制数组?

时间:2017-05-02 11:53:00

标签: matlab matlab-figure

我想在每个单位时间间隔绘制一个变量。在下面的例子中,我想在时间等于1,2,3,4和5(但不是时间等于1.25,1.5,1.75,2.25等)时绘制x。例如,可以将x阵列保存为等于1,2,3,4和5的时间,然后绘制这个与时间的关系(所以我们应该在同一图中得到五个图形)?

dtime=0.25;         % time-step
NTime=5/dtime;      % Number of time-steps

dspace=0.5;         % Distance between each x-values
NSpace=10/dspace;   % Number of x-values at each time-steps

x_old=ones(NSpace,1);
for j=1:NTime
for i=1:NSpace
x(i,1)=x_old(i,1)*5;
end
x_old=x;
end

1 个答案:

答案 0 :(得分:1)

是的,这是可能的。尝试类似:

dtime=0.25;         % time-step
NTime=5/dtime;      % Number of time-steps

dspace=0.5;         % Distance between each x-values
NSpace=10/dspace;   % Number of x-values at each time-steps

x_old=ones(NSpace,1);
figure; %create a figure
for j=1:NTime
    for i=1:NSpace
        x(i,1)=x_old(i,1)*5;
    end

    % logic to find right time and plot
    current_time = j * dtime;
    if ~isempty(find(current_time == [1,2,3,4,5] ))
        plot(1:dspace:NSpace, x) % you might have to change this depending on what you want to plot
        hold on;
    end
x_old=x;
end