Matlab 如何以单位时间间隔从for循环保存和打印数组?

Matlab 如何以单位时间间隔从for循环保存和打印数组?,matlab,matlab-figure,Matlab,Matlab Figure,我想在每个单位时间间隔绘制一个变量。因此,在下面的示例中,我希望在时间等于1、2、3、4和5时绘制x(但时间不等于1.25、1.5、1.75、2.25等等)。例如,是否可以将x阵列保存为等于1、2、3、4和5的时间,然后绘制此与时间的关系图(因此,我们应该在同一个图中获得五个图形) 是的,有可能。尝试以下方法: dtime=0.25; % time-step NTime=5/dtime; % Number of time-steps dspace=0.5;

我想在每个单位时间间隔绘制一个变量。因此,在下面的示例中,我希望在时间等于1、2、3、4和5时绘制x(但时间不等于1.25、1.5、1.75、2.25等等)。例如,是否可以将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);
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
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