MATLAB Answers

how to save all iteration output to plot graph later ?

Latest activity Commented on by dpb
on 25 Aug 2019 at 21:43
Hi,
I have 21 matrices outputs from a for loop of matrix size 21X21. i want to extract first row of all matrix from the output for each iteration and save it later to plot graph. when i plot graph, only the final output is plotted and not all the iteration output.when i save the output by indexing it says subscript mismatch error. can anyone help me how to index the bending output and get it later for plotting.
for i = 1:21
delP = P0+a*vel(:,i)+b*accl(:,i);
delu = Kcap\delP;
delv = a1*delu-a4*vel(:,i)-a6*accl(:,i) ;
dela = a2*delu-a3*vel(:,i)-a5*accl(:,i);
depl(:,i+1) = depl(:,i)+delu ;
bending(i) = depl(1,:);
end

  0 Comments

Sign in to comment.

1 Answer

Answer by dpb
on 25 Aug 2019 at 15:52

In bending(i) = depl(1,:); you don't have but one index dimension on LHS in which you're trying to store a vector...
Before the loop define/preallocate
bending=zeros(21); % creates square array of zeros for later
for i=1:21
...
bending(i,:)=depl(1,:); % save first row to array
end
I would strongly recommend to move the "magic number" 21 out of the explicit use as a constant and to use the size function to determine the limits. Then you can generalize the code and not have to make edit changes by hand should you decide to change the dimensions.

  2 Comments

hi,
I changed the code but still i am getting the error in bending (i,:) as Assignment has more non-singleton rhs dimensions than non-singleton subscripts.
dpb
on 25 Aug 2019 at 21:43
Let's see what you changed it to and the complete error in context...cut and paste all the red text; don't try to paraphrase.
Looks like what I wrote should work just fine if what you have matches what you posted.
Altho never hurt to set a breakpoint at that line and then from the command window show us what
whos depl
returns.
But, that aside
depl(:,i+1) = depl(:,i)+delu ;
will actually create 22 columns for depl when i=21 so the size of the second dimension of the bending array should be 22, not 21.
Also, we don't actually know what size(vel,1) returns, so that there are only 21 rows was an unjustified assumption--the array needs to be that long.
But, that's a dimensions error, not number of non-singleton dimensions error so something else isn't as it seems.

Sign in to comment.