从for循环matlab中提取数据

时间:2017-10-11 23:56:01

标签: matlab

我试图在这里解决的问题是如何提取x1,y1,x2,y2的数据,因为我有5组这些数据由于5个平面?进入工作区的数据只是最后一个数据,这对我来说是一个挑战,需要检索所需的数据

 j = 2;

 for i = 1 : 5 % number of planes( 3D matrix ) 
    x1 = P(:,1,i); % x value
    y1 = P(:,2,i); % y value

    valid1 = y1 > 0;
    valid_fzn1 = y1(valid1);
    valid_time1 = x1(valid1);

    x2 = P(:,3,i);
    y2 = P(:,4,i);

    valid2 = y2 > 0;
    valid_fzn2 = y2(valid2); % getting data more than zero
    valid_time2 = x2(valid2); % getting data more than zero

    figure(j)
    subplot(2,1,1)
    plot(valid_time1,valid_fzn1,'b.')
    title('Fzn vs time(Left Leg)')
    xlabel('time(s)')
    ylabel('Fzn(N)')
    hold on

    subplot(2,1,2)
    plot(valid_time2,valid_fzn2,'b.')
    title('Fzn vs time(Left Leg)')
    xlabel('time(s)')
    ylabel('Fzn(N)')
    hold on


    j = j + 1;
    i = i + 1;
end

1 个答案:

答案 0 :(得分:0)

您可以将您感兴趣的变量转换为单元格,以便能够存储另一个'层?信息。在您的情况下,我相信以下内容将起作用(请注意,我不能在不知道P的情况下看到工作结果):

j = 2;

for i = 1 : 5 % number of planes( 3D matrix )
    x1{i} = P(:,1,i); % x value
    y1{i} = P(:,2,i); % y value

    valid1 = y1{i} > 0;
    valid_fzn1 = y1{i}(valid1);
    valid_time1 = x1{i}(valid1);

    x2{i} = P(:,3,i);
    y2{i} = P(:,4,i);

    valid2 = y2{i} > 0;
    valid_fzn2 = y2{i}(valid2); % getting data more than zero
    valid_time2 = x2{i}(valid2); % getting data more than zero

    figure(j)
    subplot(2,1,1)
    plot(valid_time1,valid_fzn1,'b.')
    title('Fzn vs time(Left Leg)')
    xlabel('time(s)')
    ylabel('Fzn(N)')
    hold on

    subplot(2,1,2)
    plot(valid_time2,valid_fzn2,'b.')
    title('Fzn vs time(Left Leg)')
    xlabel('time(s)')
    ylabel('Fzn(N)')
    hold on


    j = j + 1;
    i = i + 1;
end