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

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 comentario

"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.

Iniciar sesión para comentar.

Respuestas (1)

Stephen23
Stephen23 el 11 de Sept. de 2018
Editada: Stephen23 el 14 de Sept. de 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 comentarios

I am sorry. I have tried working with your example, but with no success. Could you take a look at my code?
Stephen23
Stephen23 el 14 de Sept. de 2018
Editada: Stephen23 el 14 de Sept. de 2018
@Austin Sowers: I have no idea if that is what you need, because your explanation does not match your code: nowhere in your code can I find any loop which matches "There is an "hjunc" (line 221) array that iterates 7 times with my data": I can't find any loop which would obviously iterate seven times. Most likely you should use the solution I gave in my answer.
That still only provides me with one set of data (1x2000) for hjunc. I would like 7 sets of them.
Stephen23
Stephen23 el 14 de Sept. de 2018
Editada: Stephen23 el 14 de Sept. de 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.

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2016a

Preguntada:

el 11 de Sept. de 2018

Editada:

el 14 de Sept. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by