How can I save the differents values of a variable which is into a for loop?

3 views (last 30 days)
Hi, someone can help me with this, I think it has to be easy, but I couldn´t have found the solution.
I need for the following Script, that te value of "K" would be save each script´s loop, with names for example like k1,k2,k3... etc, becuase i need this values for another process, thank you.
for iel=1:nel
index=feeldof(nd,nnel,ndof);
k=fetruss2(el,leng,area,0,beta,1);
kk=feasmbl1(kk,k,index);
kkcf=kk;
end
I´ll be more than glad if someone can help me !!
  1 Comment
Stephen23
Stephen23 on 9 Feb 2020
Edited: Stephen23 on 9 Feb 2020
"...with names for example like k1,k2,k3... etc."
Do NOT do that.
Putting numbers into variable names is a sign that you are doing seomthing wrong:
Indexing is neat, simple, easy to debug, and very effiicent (unlike what you are trying to do). All you need to do is preallocate an output array of the right size (you did not tell us what sizes the badly-named k or kk have), and then use indexing to allocate those values on each iteration.

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 9 Feb 2020
Edited: Stephen23 on 9 Feb 2020
Just use indexing, e.g.:
Ckk = cell(1,nel);
for iel = 1:nel
... your code
Ckk{iel} = ... variable you want to store
end

Categories

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

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!