Clear Filters
Clear Filters

Building a code that saves the output of each loop as a vector

2 views (last 30 days)
I am new to MATLAB, I am trying to build a code that saves the output of each loop as a vector, i.e after making 40 loops I should have 40 vectors (each vector has different length) so that I can do further analysis. Please help?
E = 215000
K = 853
n = .166666666666666666666666666667
incr = 5
peaks = 10
stress = [0 20 -160 150 -40 330 -40 170 -20 180]
if stress(2)<stress(1)
incr = - incr
end
stress12 = stress(1):incr:stress(2)
strain12 = (stress12/E) + (stress12/K).^(1/n)
conststress = stress12(end)
conststrain = strain12(end)
plot(strain12,stress12)
hold on
for j = 2:1:peaks-1
k = j+1;
if stress(k)<stress(j)
incr = - incr;
stressjk = stress(j):incr:stress(k)
strainjk = conststrain+(((stressjk-conststress)/E)-2*((stressjk-conststress)/(2*K)).^(1/n))
conststress = stressjk(end)
conststrain = strainjk(end)
else
incr = abs(incr)
stressjk = stress(j):incr:stress(k)
strainjk = (((stressjk-conststress)/E)+2*((stressjk-conststress)/(2*K)).^(1/n))+conststrain
conststress = stressjk(end)
conststrain = strainjk(end)
end
plot(strainjk,stressjk)
hold on
end

Accepted Answer

Abhimanyu  Kashyap
Abhimanyu Kashyap on 14 May 2013
Edited: Abhimanyu Kashyap on 14 May 2013
Hi, Please post your code linewise. Right now its difficult to understand. But from what you said I gather, you want to save stresses/forces that comes out as vector in one loop. You want to carry out 40 loops and each time save that vector of forces/stress whatever. To do this, cells are very convenient..
suppose F= vector of forces.
>>force_cell= cell(1,40); % initialize a cellarray containing 40 cells.
>>for i=1:40
force_cell{i}= F ;
end
Now to you have all 40 vectors stored in cells. Suppose you want to call lets say 25th force vector. do this,
>>force_cell{25}
answer will show "F" vector corresponding to 25th loop.
Hope this helps.. Cheers..!
  1 Comment
Mohammed
Mohammed on 14 May 2013
Thank you very much for your help, I did what you suggested and it looks working. I think now I can proceed doing further analysis.
Regards

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!