combining (concat) matrix in loop to get desired output plot

1 view (last 30 days)
Hi,
I have two plots gotten from (general_load and special_load), they are time vs force. I want to create a loop where I can concat/merge the special load multiple times into the general load where N is the first position on the general_load and i is the number of times (the distance between the special loads must be N) .
Here is where it gets tricky since I want to keep the plots intact the time must be adjusted according ( eg, when the 1st special_load is added at t=50sec to the general load the entire time column in the matrix must be added by 50 such that there is a seamless connection.
A=xlsread('general_load.xlsx');
B=xlsread('test_gust_special_load');
a_length = A(end,1);
b_length = B(end,1);
N = 10;
%% for one special load in the general i used this approach
C1 = A(A(:,1)<=N,:);
C2 =[B(:,1)+N,B(:,2)];
C3 =A(A(:,1)>N,:);
C4=[C3(:,1)+b_length,C3(:,2)];
CT = vertcat(C1,C2);
%% but I am unable to get the for loop
for i= C4(1,1)+N:N:a_length
P1 =C4(C4(:,1)<=i,:);
P2 =[B(:,1)+i,B(:,2)];
PT = vertcat(P1,P2);
c_length=PT(end,1);
C4=PT(PT(:,1)+c_length,PT(:,2));
end
Z = vertcat(CT,C4);
plot(Z(:,1),Z(:,2));

Answers (1)

Bob Thompson
Bob Thompson on 20 Feb 2019
Edited: Bob Thompson on 20 Feb 2019
I suspect the issue you're having is because you're using the same C4 without attaching it to Z each time in the loop. This means that instead of attaching each segment of your special and general you only get a final segment. If this is incorrect, then please feel free to expand what you mean by 'I'm unable to get the for loop'.
Glancing over your work I would suggest these changes.
A=xlsread('general_load.xlsx');
B=xlsread('test_gust_special_load');
a_length = A(end,1);
b_length = B(end,1);
N = 10:10:a_length; % I think it's worth it to just start here.
% You don't need to do the first round outside the loop.
mark(1) = 0; % Keep track of where we are in the A matrix
mark(2) = 0; % Keep track of new time values
C = []; % Initialize your end matrix
for i = 1:length(N)
A_seg = A( A(:,1) > mark(1) & A(:,1) <= N(i) , :);
mark(1) = A_seg(end,1);
A_seg(:,1) = A_seg(:,1) + mark(2);
B_seg = B;
B_seg(:,1) = B_seg(:,1) + A_seg(end,1);
C = [C;A_seg;B_seg];
mark(2) = B(end,1)*i; % EDIT**
end
  5 Comments
Bob Thompson
Bob Thompson on 20 Feb 2019
Made another edit to the same line, see the original answer.
liju Abraham
liju Abraham on 19 Mar 2019
Hi bob,
Sorry for the late reply. But I think the error still persists even with the edit
*The distance between the special loads are still not equal, especially with smaller interval times
Thanks you for your support

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!