How can I save each workspace array (hjunc) that iterates in my code?

1 view (last 30 days)
There is an "hjunc" (line 221) array that iterates 7 times with my data. Each iteration has 2000 data points. I would like to store each iteration as an array with "1x2000 1x2000 1x2000 1x2000 1x2000 1x2000 1x2000." Or, an even better option would be if I could store each iteration as a new workspace name (i.e. hjunc1, hjunc2, hjuncN...). I have provided my code and the hjunc variable is on line 221. To run it, it requires "nodes.txt" and "pipes.txt," also provided. I am an amateur MATLAB user who is using it for engineering purposes. Need help!
Thanks in advance!
Need anything else? let me know...
My MATLAB is 2016.
  1 Comment
Stephen23
Stephen23 on 11 Sep 2018
"Or, an even better option would be if I could store each iteration as a new workspace name (i.e. hjunc1, hjunc2, hjuncN...)."
No, that would be about the worst option. Indexing is much simpler and much more efficient than magically accessing variable names. In fact the MATLAB documentation specifically advises against magically defining variable names like that: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array."
Magically accessing variable names is one way the beginners force themselves into writing slow, complex, buggy code that is hard to debug. Read this to know why:
You should just use (very efficient) indexing, with a cell array or an ND numeric array.

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 11 Sep 2018
Edited: Stephen23 on 14 Sep 2018
"How can I save each workspace array (hjunc) that iterates in my code?"
C = cell(1,7)
for k = 1:7
... your code ...
C{k} = hjunc; % or any array
end
You can easily change the dimensions of C to save more arrays from each iteration. Or use an ND array instead of a cell array. Or use a structure, if that would be clearer. All of these will be simpler and much more efficient than creating variable names like "hjunc1, hjunc2, hjuncN...)".
  4 Comments
Austin Sowers
Austin Sowers on 14 Sep 2018
That still only provides me with one set of data (1x2000) for hjunc. I would like 7 sets of them.
Stephen23
Stephen23 on 14 Sep 2018
Edited: Stephen23 on 14 Sep 2018
"That still only provides me with one set of data (1x2000) for hjunc. I would like 7 sets of them."
Unfortunately there is nothing in your code that iterates seven times, as far as I can see, so it is not clear what your comment relates to. In any case, the solution in my answer will probably do exactly what you want: use a loop that iterates seven times, each time generate some data (could be hjunc, or whatever), allocate this to the cell array C. The outputs of the seven iterations will be in C.

Sign in to comment.

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!